Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a word in Vim (and all of its other occurrences)

Tags:

vim

I frequently use the combination c-a-w to change a word in Vim.

Are there any similar means by which one can quickly also change all other occurrences of said word in the specific file?

like image 420
Mr. Nicky Avatar asked Sep 05 '25 03:09

Mr. Nicky


2 Answers

Use gn option for this purpose, in my case, I have a slightly different version of it

" allows me to use a smarter cgn
nnoremap c* *<c-o>cgn
nnoremap c# #<C-o>cgn

Now when you have to change a word many times, as long as you have not so many of it, because in this case, a classical substitution would be better, just press c* and then press "dot --> ." to change the next occurrencies of it.

If you want to see how awesomeness gn can give us have a look at: Operating on search matches using gn (vimcasts)

like image 73
SergioAraujo Avatar answered Sep 07 '25 19:09

SergioAraujo


You could try:

%s/<CTRL-R><CTRL-W>/NewWord/g

<CTRL-R><CTRL-W> means keep control key pressed and hit R and W. This copies the word under the cursor to the command line.

See :help c_CTRL-R_CTRL-W.

like image 31
Ralf Avatar answered Sep 07 '25 20:09

Ralf