Confirm replacements
Vim has a very robust search and replace capability with the substitution :s
command. We’ve written plenty of tips about subtitiution before but one I’ve been using more frequently recently is the /c
flag to confirm each replacement.
:%s/foo/bar/c
– Substitute foo with bar in the whole file but prompt for Confirmation before each replacement.
With the /c
flag appended to your substitution command, Vim will successively iterate over each matching pattern. It will pause to ask you if you want to make each replacement:
replace with bar (y/n/a/q/l/^E/^Y)?
There’s a lot of options there so let’s enumerate each of them:
y
– Yes, make the substitutionn
– No, skip to the nexta
– Replace All the remaining matches without confirmationq
– Quit substitution and make no further changesl
– Last one! Make this change, but quit after this one^E
– Press<ctrl-e>
to scroll down in the file and get a better view of the surrounding lines^Y
– Press<ctrl-y>
to scroll up