Reputation: 3
'Enveloping' a word or a string in a given character is one of my most executed actions in VIM.
Most common char combinations (added at beginning and end, respectively): <>, (), '', "", **, __,--, etc...
Is there a native way of doing this in VIM? If not, how could we set up a macro for this?
Upvotes: 0
Views: 700
Reputation: 11800
You can create a function abstracting all the chang inner word:
if !exists('*AddSingleQuotes')
function! AddSingleQuotes()
exec "normal! ciw''\<Esc>P"
endfunction
endif
nmap <Leader>q :call AddSingleQuotes()<CR>
And also modify to add double quotes:
if !exists('*AddDoubleQuotes')
function! AddDoubleQuotes()
exec "normal! ciw\"\"\<Esc>P"
endfunction
endif
nmap <Leader>q :call AddDoubleQuotes()<CR>
NOTE: the if !exists
part ensures vim will not load the function more than once, even if you reload your vimrc.
Upvotes: 1
Reputation: 196566
The most "native" way to surround a word with a given pair of characters is to:
ciw
(the word is put into register "
by default),"
with <C-r>"
,<Esc>
.In short:
ciw(<C-r>")<Esc>
This is the most efficient you can get without making custom mappings or installing plugins like Surround or Sandwich.
Upvotes: 2