Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search and replace word under cursor in Vim [duplicate]

Tags:

vim

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?

like image 627
user1728853 Avatar asked Mar 14 '13 20:03

user1728853


1 Answers

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/
like image 122
romainl Avatar answered Oct 20 '22 14:10

romainl