Vim: Custom mapping for surround plugin to surround visual selection?

631 views Asked by At

I intend to surround my visual selection, same as sublime and other editors.

Since I already have surround plugin installed, therefore viwS" does surrounds the selected word with " and similarly for ', etc

Therefore following is my attempt :

vnoremap ' S'
vnoremap " S"
vnoremap [ S[
vnoremap { S{
vnoremap ( S(

But mysteriously it deletes the entire line and inserts the quote when I try using them.

Where am I going wrong ? How to correct it ?

1

There are 1 answers

5
romainl On BEST ANSWER

You are using *noremap wrong:

When you use *noremap, you ask Vim to use the original meaning of S.

When you use *map, you ask Vim to use whatever is the current meaning of S.

vnoremap is OK if the commands executed by your mapping are supposed to be native commands but you are using a command that is already remapped by a plugin. You should use *map, instead.

Additionally, vmap covers visual and select mode: you should probably be more precise in your mappings.

So…

xmap ' S'
xmap " S"
xmap [ S[
xmap { S{
xmap ( S(

(edit)

Your problem with <C-c> is the exact reverse: you are using vmap so you are telling Vim to use whatever " currently does. Because…

  1. you have mapped " to do S",
  2. you use " in the RHS of your <C-c> mapping,
  3. and you use vmap for it…

pressing <C-c> does S"+ygv which is not what you want. The rules above are simple: follow them and all will be good. Here they are again, with a slightly different wording:

  • Use *noremap when you want to use commands with their default meaning.

  • Use *map when you want to use commands with their remapped meaning.