In Vim, replace all occurrences of the current word under the cursor

If you press * in Vim, the editor will look for the next occurrence of this word in the same file. This eliminates the need to enter a term.

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

+55
vim
Apr 04 '11 at 16:12
source share
4 answers

You can use:

:%s/<cr><cw>/new value/g 

where <cr><cw> means literally type CTRL - r CTRL - w to insert a word under the cursor.

+48
Apr 04 '11 at 18:08
source share

Just use * and then:

 :%s//new value/ 

If you do not specify a template, the substitute command uses the last one found. Depending on the gdefault value in your configuration, you may need to add the /g modifier to this command.

+88
Apr 04 '11 at 16:15
source share

In addition to the other answers, you can save a few more keystrokes by adding the following snippet to your .vimrc file to do a global search and replace.

 " Search and replace word under cursor using F4 nnoremap <F4> :%s/<cr><cw>/<cr><cw>/gc<cf>$F/i 
+11
Apr 04 2018-11-21T00:
source share

I went ahead and hacked a plugin that allows you to simply enter the following:

 :Dr REPLACEVALUE 

https://github.com/chiedojohn/vim-dr-replace

+2
May 6 '15 at 12:49
source share



All Articles