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.
/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 substitution
n– No, skip to the next
a– Replace All the remaining matches without confirmation
q– Quit substitution and make no further changes
l– Last one! Make this change, but quit after this one
<ctrl-e>to scroll down in the file and get a better view of the surrounding lines
<ctrl-y>to scroll up
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?