Kakoune: Add vim-surround-like feature

Created on 18 Dec 2016  路  10Comments  路  Source: mawww/kakoune

Since selections are so integral to Kakoune, it would be awesome to have a feature for surrounding the selection(s), and changing the surrounding characters.

See https://github.com/tpope/vim-surround

Most helpful comment

I added this feature in #1295.

All 10 comments

This is pretty easy to do by default already, using i and a. If you want to make things easier, you could implement a script that declares some bindings to quickly surround a selection, but I don't think this would be a good fit for upstream.

@lenormf could you elaborate how to change the surrounding characters using i and a? I'm pretty new to Kakoune and I had the same question as OP.

Assuming you want to surround the word under the cursor with a shell variable expansion:

<a-i>w
i ${ <esc>
a } <esc>

i inserts things at the beginning of the selection, a extend the selection by its end.

I see. Is there also a way to do the second thing i.e. changing the surrounding characters? For example if I have

"something quoted"

I'd like to change the double quotes to single quotes to make it

'something quoted'

As an aside, I'm fine with this being in a separate script but it's such a useful and well-used feature that I would second it being considered to be in core. This would make it different from vim though, where it is an external plugin.

I think I figured out the answer to my question above. I can do this by selecting around the quotes (alt+a ") then splitting to multiple cursors on the quote (s") and then changing them to single quotes (c').

r' would keep you in normal mode, but c' works as well.

Thanks for the tips, all. I need more practice using the kakoune way. It would be great for more experienced users to share some of these usage patterns in the wiki :)

In case future generations find this, you can add this to your kakrc to get behavior very similar to vim-surround:

def surround %!on-key %@exec %sh&
  case "$kak_key" in
  "<lt>") key="<" ;;
  "<gt>") key=">" ;;
  "<space>") key=" " ;;
  "<tab>") key="\t" ;;
  \<*\>) echo ":echo<space>no<ret>"; exit 1 ;;
  *) key="$kak_key" ;;
  esac

  open="$key"
  close="$key"
  case "$key" in
  ")") open="(" ;;
  "]") open="[" ;;
  "}") open="{" ;;
  ">") open="<" ;;
  "(") open="( "; close=" )" ;;
  "[") open="[ "; close=" ]" ;;
  "{") open="{ "; close=" }" ;;
  "<") open="< "; close=" >" ;;
  esac

  epilogue=${close//?/H}

  open=${open/</<lt>}
  close=${close/>/<gt>}

  open=${open/ /<space>}
  close=${close/ /<space>}

  echo "i$open<esc>a$close<esc>$epilogue"
&@!

map global user s ":surround<ret>"

Now you can use, e.g. ,s) to surround the selection with matching parens, or ,s* to surround with asterisks.

(Improvements welcome -- the odd nested string quoting is particularly painful.)

Great, thanks ianthehenry. I have added your snippet to the wiki : https://github.com/mawww/kakoune/wiki/Surround-selections . This way it can be tweak easily by anyone.

I added this feature in #1295.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vbauerster picture vbauerster  路  3Comments

hwmack picture hwmack  路  4Comments

radare picture radare  路  3Comments

MasterOfTheTiger picture MasterOfTheTiger  路  4Comments

valerdi picture valerdi  路  4Comments