sites
sites

Reputation: 21795

Surrounding in vim

Is there a simple way to surround text, I know about this plugin

But too much keys.

I tried my approach

vmap ' <D-x>i'<esc><D-v>i'
vmap " <D-x>i"<esc><D-v>i"

But this does not work. Anyway if this way is fixed how could I extract this behaviour to a function that allows me this:

  1. I select any text (I think this would be visual mode)
  2. I enter any symbol in a given set for "symmetric" surround (for example ', ", `, |, /, , *, -), or for "asymmetric" surround (for example <>, {}, [], ())
  3. selected text gets surrounded by the symbol.
  4. Maybe would be good to detec if selected word is already surrounded and toggle symbol.

Any recommended reading on how to do this?

Upvotes: 0

Views: 126

Answers (3)

Ingo Karkat
Ingo Karkat

Reputation: 172590

If you like the surround plugin, but prefer shorter mappings, you can just make your own:

:vmap ' S'
:vmap " S"

(Here, you have to use :vmap over the preferred :vnoremap, because you want the plugin's mappings to apply. Alternatively, you can directly map to the plugin's <Plug> mappings: :vmap ' <Plug>VSurround')

Upvotes: 3

Tassos
Tassos

Reputation: 3288

This is what I would do:

:vmap ' s''<esc>P

Upvotes: 1

romainl
romainl

Reputation: 196566

s' and s" are "too much keys"? Really?

Surround is the way to go.

You won't get far with a mapping (and one that uses the Cmd key). If you want a "smart" command that toggles quotes you can't really skip vimscript and, from there, it's probably a better idea to just use Surround which is one of the greatest Vim plugins.

Anyway, this could be a (quick and not thoroughly tested) basis:

function! SurroundWithDoubleQuotes()
  let old_n = @n
  normal! gv
  normal! "nd
  let @n = "\"" . @n . "\""
  normal! "nP
  let @n = old_n
endfunction

You would need to find a way to enter the desired character and test for the presence of that character at both ends of the @n register.

:h functions will definitely help you if you want to go that way.

Here is how I would do with a mapping:

vnoremap " <Esc>`>a"<Esc>`<i"<Esc>

`> marks the end of the visual selection
`< marks its beginning

but be careful with your mappings. In visual mode, (){}[] have meanings that you don't want to overload.

Upvotes: 6

Related Questions