Tip 90Eyeball Each Substitution

Finding all occurrences of a pattern and blindly replacing them with something else won’t always work. Sometimes we need to look at each match and decide if it should be substituted. The c flag modifies the :substitute command to make this possible.

Remember this example from Tip 5?

 ...We're waiting for content before the site can go live...
 ...If you are content with this, let's go ahead with it...
 ...We'll launch as soon as we have the content...

We couldn’t use find and replace to change “content” to “copy.” Instead, we used the Dot Formula to solve our problem. However, we could also have used the c flag on the substitute command:

=> :%s/content/copy/gc

The c flag ...

Get Practical Vim, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.