Goal: Select multiple words, one at a time, then replace them all.
I'm new to Vim; I come from Visual Studio. I would like to achieve one important feature that helped me a lot in my productivity: Select multiple words, one at a time, and then replace them all.
I know there is a Vim way of replacing using:
:% s/target-word/replacement/gc
But as you can see it's still a lot to write compared to its counterpart in VS Code. Could someone help me with a vim script or a plugin to achieve this in the most similar way?
- imagine that I already have selected the word with
viw
, for example. - then I want to have a single command to select the next match.
- then start writing and see those changes written in real-time or after hitting
Esc
.
[Edit] Accepted answer
Thanks D. Ben Knoble; you provided to me the more convenient and practical answer. As the main goal is to change a word locally, the combination *cgn
Is perfect.
As I'm changing the also the current word and *
jumps me to the next match, what I add to go back to that word is Ctrl + N
.
* Ctrl + N cgn
then .
:grep -R match
, depending ongrepprg
(I use ripgrep, so it’s really easy to do such a project wide search),:tselect
if you have tags setup,:find
if you know the file... there are many ways to search and navigate :)"ayiw
to yank the word to registera
, I mainly increment the number by a random number then do"byiw
. Then I use%s/ (CTRL-ra)/(CTRL-rb)/gc
where(CTRL-ra)
puts register a into the search part of the replace. Thengc
goes match by match and you can replace or not. Generally I replace all because I know I need to. If i need to check they changed I use:g//
and expect no matches. Not a dev so maybe not what you're looking for.N
(shift-n) instead of Ctrl-N?"a[pP]
etc. Copying to a register in:g
is useful as well eg:g/{pattern}/y A will append matches in register a. You have to clear the register first with
qa` or `:let @a=''`` . learnvimscriptthehardway.stevelosh.com/chapters/12.html might help