Confirm replacements

4.5
(57)

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/cSubstitute 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:

  • yYes, make the substitution
  • nNo, skip to the next
  • a – Replace All the remaining matches without confirmation
  • qQuit substitution and make no further changes
  • lLast 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

How useful was this tip?

Average rating 4.5 / 5. Vote count: 57

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

105 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts