Case-preserving find and replace

3.8
(299)

One of the many useful features of Tim Pope’s Abolish plugin is the ability to preserve case when finding and replacing text.

We already know that text substitution is simple in Vim:

:%s/from_text/to_text/g

But what if we have varying case? Take for example, you’re renaming an entity in an application from Company to Organization. You might have a file with references to both company and Company. Instead of doing two substitutions, one for each, you can do them both at the same time with Abolish.

In fact, the only difference you need to remember is to use capital S instead of lowercase s:

:%S/company/organization/g

That’s it!

References to company will be replaced with organization, and Company with Organization. It will even handle camel case and other capitalization syntaxes.

Here’s another example where all the variations in case of Product was changed to Variant in one simple command. This can be extremely useful during refactoring or when copying boilerplate code, such as this simple Rails controller.

:%S/product/variant/g

How useful was this tip?

Average rating 3.8 / 5. Vote count: 299

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

81 Posts

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

Leave a reply

Your email address will not be published. Required fields are marked *