Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

Supercharged substitution with :Subvert

#48

Run time:

The abolish plugin provides a command called :Subvert, which is like a supercharged version of Vim’s built-in :substitute command. The :Subvert command is especially useful for changing singular and plural variants of a word, and for refactoring names that appear in snake_case and MixedCase.

This is part two of a three-part series on Tim Pope’s abolish plugin.

Shownotes

In Episode 47, we saw how the :Subvert command can be used as a smart search command. In this episode, we learn about this form of the :Subvert command:

:[range]Subvert/target/replacement/[flags]

:Subvert behaves a lot like Vim’s standard :substitute command: it accepts the same flags and operates the same way over a range, but the target and replacement fields are handled differently.

Suppose that we wanted to change the word pumpkin to potato. We could use the :Subvert command:

:%S/pumpkin/potato/g

This looks a lot like Vim’s built-in :substitute command. In fact, if we used a lowercase s then this would be a valid :substitute command. But :Subvert does a bit of extra work for us. Not only will it change pumpkin to potato, it will also change Pumpkin to Potato, and even PUMPKIN to POTATO.

Working with irregular plurals

The plural form of potato is irregular: it ends with -es. We can adapt the :Subvert command from above to handle singular and plural forms like this:

:%S/pumpkin{,s}/potato{,es}/gc

This will change pumpkins to potatoes and pumpkin to potato. That’s pretty handy!

We could also use the :Subvert command to switch from a word with irregular singular/plural forms to a regular word. Suppose we needed to change mouse to trackpad. The :Subvert command could handle it like this:

%S/m{ouse,ice}/trackpad{,s}/gc

This command will change mouse to trackpad and mice to trackpads. As before, it will work for Titlecase and SHOUTYCASE variants too.

Working with snake_case and MixedCase

The :Subvert command can also target snake_case and MixedCase forms of a word. Suppose that we needed to change insert_mode to replace_mode. We could use this command:

:%S/insert_mode/replace_mode/gc

Not only will this change insert_mode to replace_mode, but it will change InsertMode to ReplaceMode, and INSERT_MODE to REPLACE_MODE as well. That can be really handy when refactoring code.

Swapping words

We could use the :Subvert command to swap all occurrences of vim and tmux in this tweet:

:S/{vim,tmux}/{tmux,vim}/g

That would change each instance of vim to tmux and vice versa.

Further reading

Comments

Level-up your Vim

Training

Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..