Vim search and replace selected text

Try execute the following or put in into your .vimrc

vnoremap <C-r> "hy:%s/<C-r>h//gc<left><left><left>

By pressing ctrl+r in visual mode, you will be prompted to enter text to replace with. Press enter and then confirm each change you agree with y or decline with n.

This command will override your register h so you can choose other one (by changing h in the command above to another lower case letter) that you don't use.


This quick and simple mapping search for visually highlighted text (without over-writing the h register) and without using the terminal dependant + register:

" search for visually hightlighted text
vnoremap <c-f> y<ESC>/<c-r>"<CR>   

If you dont like the ctrl-f change it to your preference

Once the text is highlighted you can always do a substitution simply by typing:

%s//<your-replacement-string>

... because a blank search on the s command uses the last searched for string.