Vim mapping to surround word with a character -


in vimrc file have following entry surround word '+':

:nnoremap <silent> q+ wbi+<esc>ea+<esc> 

i change surround word other symbol, example quote, not use plugin or insert 1 more map. in vimrc:

:nnoremap <silent> qx wbix<esc>eax<esc> 

where x character put around word.

how can done in vim?

thanks

the general idea:

:nnoremap q :let c=nr2char(getchar())\|:exec "normal wbi".c."\eea".c."\e"<cr> 

but don't think q choice, example quotes, because q starts extremely handy recording feature vim. normal q command expect second character (the recording target register) , can ". quite confusing when you're on computer or if else using vim. should preprend leader q. leader:

:nnoremap <leader>q :let c=nr2char(getchar())\|:exec "normal wbi".c."\eea".c."\e"<cr> 

the default leader value \, can changed. note had changed before defining mapping. can see configured leader let mapleader (which prints error message if there's no leader configured). these statements,

:let mapleader=',' :nnoremap <leader>q :let c=nr2char(getchar())\|:exec "normal wbi".c."\eea".c."\e"<cr> 

you can type ,q+ surround word + , ,q" in order surround word ".


by way: mapping doesn't work if cursor on last character on line. change mapping to:

:nnoremap <leader>q :let c=nr2char(getchar())\|:exec "normal viwo\ei".c."\eea".c."\e"<cr> 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -