1

Apologies I am probably expressing myself very badly, that's also why 1 hour searching hasn't yielded the result I wanted yet...

Assume I want to replace true with false in a buffer.

:s/true/false

Now, this is such a common word that I don't want to just change globally :%s/true/false/g. I want to move the cursor (or search for the word), and every time I want to actually change, repeat the substitution.

So I can do then :<up arrow to show again the substitution command><CR>

If I'd like to map this to a keyboard shortcut:

map <C-Y> :<WHAT TO PUT HERE?><CR> ?

I tried map <C-Y> :@<CR> but that doesn't seem to be doing what I want.

1 Answer 1

3

No mapping necessary!

Vim has the & command which just repeats the last substitution. The same way as . repeats the last command, I can move to the next true and simply hit & which will apply again the last substitution. Neat and simple.

I found out by googling “vi repeat last substitution” which yielded this SO post as the first result.

2
  • 2
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 21, 2021 at 2:26
  • 2
    In specific, you don't need to go into details on which keywords led you to the answer... Just explain what it is that works to resolve your problem. Feel free to keep the reference to the Stack Overflow answer that helped you (but I suggest moving it to the end of the answer, as it's not really the most important part...)
    – filbranden
    Dec 21, 2021 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.