Surrounding a selection with something

I have a selected a word and I want to surround it with some quotes. I figured I could use S with the \b regex and I would end up with two selections, one at the beginning of the word and the other at the end. Alas, no luck. How should I do this?

Hi. Have a look at this plugin that does exactly that for inspiration: https://github.com/Delapouite/kakoune-mirror

The easiest thing to do is probably:

i"<esc>a"<esc>

If it’s a thing you do often, you could stick it in a mapping or a command.

It’s tempting to think of making zero-length selections at the beginning and end of the word and inserting a quote into both of them at the same time, but Kakoune’s editing model doesn’t really allow zero-length selections, so that’s not a practical solution.

surround.kak supports free-typing through hooks.

Here is the gist you can add to your kakrc if you don’t want plugins:

  # Enter insert mode
  define-command surround-enter-insert-mode -docstring 'Enter insert mode' %{
    execute-keys -save-regs '' 'Z'
    hook -always -once window ModeChange 'pop:insert:normal' %{
      hook -always -once window ModeChange 'pop:insert:normal' %{
        execute-keys z
        set-register ^
        echo
      }
      execute-keys -with-hooks a
    }
    execute-keys -with-hooks i
  }