Vim Tips Wiki
Tip 409 Printable Monobook Previous Next

created January 21, 2003 · complexity basic · author Lyle · version 5.7

I wish to use selected text as part of a command - say, to substitute instances of a very long variable with a shorter name:

:%s/<selected text>/ShorterName/g

How do I do that?


  1. yank your selected text using the 'y' key.
  2. when entering your command you can use control-r followed by " whenever you need your selections. (control-r " inserts the contents of the "-register which is the standard register for yanking)
  3. in this specific case where you want to search for the selected text and replace it, you can do that by hitting * to search for instances of selected text, and then just replace the last searched item like :%s//replacement/g

Just put the cursor over the word in your text. In the command line press <C-R><C-W> the to get the word.

See h: c_<C-R>_<C-W>

I have the following maps:

vmap ,g :s/<C-R>//
map ,g :s/<C-R>//

I then just add the closing delimiter and flags and hit return. These mappings have saved me a great deal of time.