Reputation: 21795
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:
Any recommended reading on how to do this?
Upvotes: 0
Views: 126
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
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