In Vim, replace all occurrences of current term under cursor

If you press * in Vim, the editor will search for the next occurrence of the term in the same file. It saves you from having to type out the term.

Is there a quick way to replace the term currently under the cursor with a new one? One character to issue the command, typing in the new term, then Enter.


Just use * and then:

:%s//new value/

If you don't specify a pattern, the substitute command uses the last searched one. Depending on the value of gdefault in your configuration, you might need to add a /g modifier to that command.


You can use:

:%s/<c-r><c-w>/new value/g

where <c-r><c-w> means to literally type CTRL-rCTRL-w to insert the word under the cursor.


In addition to the other answers, you could shave off some more keystrokes by adding following snippet to your .vimrc file for doing a global search and replace.

" Search and replace word under cursor using F4
nnoremap <F4> :%s/<c-r><c-w>/<c-r><c-w>/gc<c-f>$F/i