So at one point I proudly admitted to a co-worker that I knew how to search and replace in Vim: I did
bar, and escape (changing the inner word to 'bar', or whatever the right replacement motion would be) and then press
. for the number of times I needed to (first one goes to the next occurrence of 'foo', and the period key repeats the last edit, which is magic and very powerful in itself).
He replied with a less interesting, but in some cases more appropriate way of doing it:
:%s/foo/bar/gc. This command takes the current file (
%), and substitutes 'foo' for 'bar', with a flag for global (more than once) and choice. Vim will then stop at each 'foo' and gives you choices for 'y' or 'n' (and others), so you can interactively pick your replacements.
Today I leaned about the
gn text object. Where
iw stands for 'inner word',
gn stands for 'go next'. So one can type
bar, and escape, which is almost the same. But then you can just keep hitting
., and it will perform the last action on the next occurrence of 'foo'. No need for
I mean if you want to be presented with a choice for each replacement, go use
:%s, but this
gn thing is darn cool.
Edit: just noticed that the
c flag makes the
g superfluous. Got to tell my co-worker tomorrow.