Supercharged substitution with :Subvert

#48

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.

Suppose that we we wanted to change pumpkins into potatoes. Let’s use the :Subvert command to do it.

We’ll target the word pumpkin and replace it with potato. We’ll use the g and c flags, which behave just the same way as they would with a regular substitute command.

:%Subvert/pumpkin/potato/gc

Before I hit enter, note that I’ve supplied the target and replacement strings in lowercase. And yet, when I execute the command, it picks up the occurrence of Pumpkin that begins with an uppercase letter. I’ll press y to confirm that change, and again to change the second occurrence.

The plural form of potato is irregular: it ends with -es. So we’ve got a bit of tidying up to do here.

Instead of fixing it by hand, let’s undo the change, then refine our Subvert command. We can use curly braces to specify a set of alternative endings for our target and replacement words. The plural form of pumpkin simply adds the letter s, whereas potato ends with es:

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

That command correctly handles both the singular and plural forms.

Let’s look at another example: this time, we’ll convert the word mouse to trackpad. Once again, we’re dealing with an irregular plural: mouse becomes mice (not mouses).

We start off by specifying the part of the word that is common to both forms, which in this case is just the letter ’m'. Then in braces, we specify each alternative ending for the target and replacement:

Working with MixedCase and snake_case

The subvert command is also useful if you need to refactor a name that appears in both MixedCase and snake_case. For example, in ruby, module names and class names are given in MixedCase, while snake_case is used for naming the corresponding files.

Suppose that we wanted to change insert_mode to replace_mode. We could do it using the :Subvert command:

:%S/insert_mode/replace_mode/gc

Even though I’ve only specified the snake case format, the Subvert command automatically applies the change to the MixedCase versions as well.

Swapping words

You can also use the :Subvert command to swap all occurrences of two words. Here’s a slightly daft example: we’ll use the :Subvert command to swap all occurrences of vim and tmux in this sentence:

Anyways I made a vim plugin for tmux. I mean a tmux plugin for vim.
:S/{vim,tmux}/{tmux,vim}/g

Did you miss it? Watch, as I undo, then redo again: each occurrence of vim becomes tmux, and vice versa.

Real world example: asset migration

I’d like to finish by demonstrating how the :Subvert command helped me out recently. I was migrating the assets for a website to Amazon’s S3 storage and I had to go through all existing content to update the links to assets.

Before the migration, a typical asset path looked like this:

/assets/102/DSC_0524_normal.JPG

In this case, the nubmer 102 corresponds to the ID of that asset’s record in the database. After the migration, the path for the same asset looked like this:

On the surface, the transformation looks simple: just prepend the S3 domain in front of every /assets/ path, but there was a complication. In the process of moving to S3, each asset was assigned a new ID in the database. So I had to replace all of the old IDs with the new ones.

This should be possible using a :Subvert command of this form:

:%Subvert!assets/{former_ids}/!assets/{new_ids}/!g

Which is just a variation on the word swap example we saw a moment ago.

I kept all the information that I needed in a yaml file: which recorded the old and new ID for each asset.

I massaged these records into two blocks of comma-seperated ids: one containing all of the former_ids, the other containing all of the new_ids. I won’t show the steps involved, but this could be a classic VimGolf challenge!

Then I take this long list of former_ids and paste it into the target region of my command. Do the same for the new_ids, pasting into the replace region of the Subvert command. That looks pretty crazy, but if I’ve done it right then this huge command should replace all old asset IDs with their new equivalent.

Of course, I’m not going to type that out! Instead, I’ll yank it into register ’s', then switch to one of the markdown files to try it out. To run the command, I’ll paste register ’s' into the command line:

:%Subvert!assets/{former_ids}/!assets/{new_ids}/!g

That looks good! Let’s undo that, then run the same command across the entire arglist, which I’ve already populated with 5 markdown files.