Coming soon: Peer to Peer - watch how experts solve tech problems.

Learn more

Coming soon: Peer to Peer - watch how experts solve tech problems.

Smart search with :Subvert

#47

Run time:

The :Subvert command lets us create a particular style of regular expressions with ease. It’s great for matching irregular singular and plural words in plain English and also for variable names that come in snake_case and MixedCase forms.

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

Shownotes

The :Subvert command lets us generate a pattern based on

:Subvert /pumpkin

This has the same effect as the following search:

/\v\C%(PUMPKIN|Pumpkin|pumpkin)

The :Subvert command can also accept a comma-seperated list of alternatives wrapped in braces. These are assembled to form a pattern. For example, we could search for both ‘mouse’ and ‘mice’ by running the command:

:S/m{ouse,ice}

If we specify a pattern with words separated by underscores, the :Subvert command automatically matches the mixed case alternative too. For example:

:S/insert_mode

Would match insert_mode and InsertMode.

The :Subvert command comes in many different forms. In it’s most basic form, it resembles plain search. If you specify a file (or glob), then the command resembles :vimgrep. When a replacement field is specified, :Subvert behaves like the built-in :substitute command.

Command effect
:S[ubvert]/pattern search in the current buffer
:S[ubvert]/pattern/ {file} ... search in the specified file(s), collecting results in quickfix list
:S[ubvert]/pattern/replacement/[flags] substitute in the current buffer

Episode 48 shows how to use the :S/pattern/replacement/ form.

Coercing variable caseing

The cr mapping stands for coerce. It lets you switch between different casing styles:

Mapping effect
crc coerce to camelCase
crm coerce to MixedCase
crs (also cr_) coerce to snake_case
cru coerce to SNAKE_UPPERCASE
cr- coerce to dash-case

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..