VIM find all occurrences and replace with input

Is it possible to find occurrences of a certain word in the file and ask the user to enter a word to replace this word. Do the same again, either by typing a new replacement, or using something from your story until all words are replaced or the user logs out. Keep in mind that I want to be able to skip words that I do not want to replace.

Just using

:%s/value_one/value_two/gc 

will mean that I have to execute this command for every replacement I have in mind. Is this possible or something is wrong with this workflow. I would use this when pasting standard code that requires only one variable to be changed.

+7
source share
2 answers

You are looking for:

 %s/value_one/\=input('replace <'.submatch(0).'> with: ')/gc 

:h :s\= and :h input() . You should be able to specify a default value for input if you want.

+15
source

It may not be perfect, just share what I used.

This assumes that :set hl already installed. Pressing * will search for the word under the cursor. Hover over your word. But usually I use gd instead of * .

Then change it with ciw to the replacement word. Then Esc . Now we can move on to the next event on n .

If it needs to be changed to the same word, then press . doing this. If not, you can switch to the new value using ciw again.

If you do not intend to change the next incident, just skip it by pressing n , which will happen until the next.

This seems a little tame, but it gives conservative control over my editing.

+3
source

All Articles