Entering search patterns and replacement strings in Vim can sometimes be a pain, particularly if the search or replacement text is already available in a register or under the cursor. There are a few ways to make working with search and replace in Vim quicker and less cumbersome for some common operations, and thereby save a bit of error-prone typing.
Insert contents of a register
As in command mode or insert mode, you can insert the contents of any register
by pressing Ctrl+R and then the register key. For example, to directly insert
the contents of register a, you can type Ctrl+R and then a while typing a
search pattern or replacement string.
Reference contents of a register
Similarly, if you want to use the contents of a register in a pattern or
replacement but don’t want to directly insert it, you can instead reference
the contents of register a with \=@a:
:s/search/\=@a/
Both of the above tips work for both the search and replacement patterns, and
for special registers like " (default unnamed register) and / (last
searched text) as well as the alphabetical ones.
Insert word under cursor
If you happen to be hovering over a word that you want to use as as a search or
replacement string, as a special case of the above you can do so by typing
Ctrl+R and then Ctrl+W for a normal word, or Ctrl+R and then Ctrl+A for
a space-delimited word.
Empty search string
You can use the previous search as a search pattern or replacement string by
including it from the special / register, in the same way as any other
register above, by inserting it with Ctrl+R and then /, or representing it
with \=@/. There’s a convenient shorthand for this however in just using an
empty search string:
:s//replacement/
This will replace all occurences of the previous search string with
replacement. It turns out to be a particularly convenient shorthand when
searching for words by pressing * or #.