Search and replace a word under the cursor in Vim

I often hover over a word where I would like to replace all occurrences of that word with another word. What is the easiest way to replace all occurrences of a word under the cursor with another word?

+5
source share
1 answer

Use <C-r><C-w>to insert a word under the cursor on the command line:

:%s/<C-r><C-w>/bar/g

If you need to do this a lot, you can easily create a mapping as follows:

nnoremap <F6> :%s/<C-r><C-w>/

The one shown above inserts the first part of the command, ready for you to enter a replacement, any flag and press <CR>:

:%s/foo/
+11
source

All Articles