Hashrocket.com / blog

Large letters

Vim Magic with Abolish.vim

posted on and written by in

Image 100x100 jake worth

Occasionally, I discover a Vim plugin that blows my mind. Abolish.vim is just such a plugin.

The creator Tim Pope describes Abolish.vim as follows:

It's three superficially unrelated plugins in one that share a common theme: working with variants of a word.

He's right. The features of this plugin— abbreviation, substitution, and coercion— seem unrelated. The first can be used to preempt chronic typos, the second can be used to beat Vim's limited string substitution, and the third can be used to transform all the unique cases we use as programmers. Why combine them?

It doesn't matter why. This plugin is amazing. Add it to your Vim configuration, learn a few commands, and prepare to look like a magician at the keyboard.

In this blog post, we will explore the setup and three main features of Abolish.vim.

Setup

With Pathogen.vim, installation is as easy as:

$ cd ~/.vim/bundle
$ git clone git://github.com/tpope/vim-abolish.git

Abbreviation

Do you often type 'teh' when you meant 'the'? What about 'fucntion' instead of 'function'? These mistakes are easy to make, and sometimes for historical or cultural reasons you just can't seem to break the habit.

Keystrokes are valuable, so stop fixing the same typos. Abolish.vim can help. If you don't have an after/ directory in your '.vim' directory, create one. It's the default for the plugin. Then add a config file:

$ mkdir ~/.vim/after/plugin
$ touch ~/.vim/after/plugin/abolish.vim

The after/ location tells this code to be run after all plugins, including Abolish.vim, have been loaded. Inside your new file, add this:

" ~/.vim/plugin/abolish.vim
Abolish teh the

Restart Vim and try typing 'teh', then a space. Vim fixes it. Failure neutralized!

Here are four common misspellings of 'function' you might address:

" ~/.vim/plugin/abolish.vim
Abolish functoin function
Abolish fucnton function
Abolish fucntion function
Abolish fuction function

It even supports curly brackets, preempting multiple typos on one line:

" ~/.vim/plugin/abolish.vim
Abolish functoin{ally,ed} function{}

This fixes the stem of the words functoinally and functoined by changing them to function, while preserving the affixes ally and ed.

Tangentially related to this feature is the :Abolish -search foo command, which will find foo, Foo, and FOO.

Substitution

Do you like Vim's string substitution? Sort of? Me too. But it's an essential tool.

A weakness of this tool is how it handles case variants. :%s/FOO/BAR/g will replace FOO with BAR, and :%s/foo/bar/g will replace foo with bar, but doing both at once? That would be useful.

Abolish.vim does this with the following command, adding curly bracket support along the way:

:%Subvert/run{ning,ner}/walk{ing,er}/g

In one line:

  • running becomes walking
  • RUNNING becomes WALKING
  • Running becomes Walking
  • runner becomes walker
  • RUNNER becomes WALKER
  • Runner becomes Walker

This is crucial when changing the name of a concept that is used in a lot of places.

Coercion

When I've been working in JavaScript and switch to Ruby, the first variable I write will almost always be something like notificationEmail. And vice versa when switching the other way; that first JavaScript variable will be a Matz-approved admin_user. These mistakes violate language convention and require wasteful keystrokes to fix.

Coercion is the killer feature of Abolish.vim. crs (coerce to snake case) fixes the first example:

# some/ruby/file.rb
noticationEmail
# crs (visual mode)
notification_email

crc (coerce to camel case) fixes the second:

// some/javascript/file.js
notification_email
// crc (visual mode)
noticationEmail

crm (coerce to mixed case), cru (coerce to upper case), cr- (coerce to dash case), and cr. (coerce to dot case) are all useful in various situations.

Conclusion

Once the plugin is loaded, run :h abolish to learn more. If you've just added it, this may require running :Helptags (Pathogen users) or :helptags {dir} (everyone else) first.

In this blog post, we looked at Abolish.vim's setup and key features. We have only scratched the surface of what this plugin can do.

Add it to your Vim stack. It's one of those workhorse plugins that makes Vim more fun and powerful. Thanks to the Rocketeers of yore for adding this to the Hashrocket Dotmatrix, and to Tim Pope for publishing it to the world.

header photo via Ian Kobylanski on Flickr

Posted in Vim