Search and replace word under cursor in Vim

vim

Solution

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

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

If you need to do that a lot, you can easily create a mapping like this:

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

The mapping above inserts the first part of the command, ready for you to type the replacement, any flag and hit `<CR>`:

:%s/foo/

Problem

I frequently have my cursor on 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 the word under the cursor with another word?

Original source

Related problems