:best of Vim

Showcasing the top Vim plugins, tips and tricks.

What is Vim?

Vim is a text editor for programmers, power users & command-line junkies.

What is :best of Vim?

:best of Vim showcases the best plugins, extensions & tips for making the most of Vim.

It was built for NoVIMber 2011.

Learn more...

Follow

SnipMate

Every programming language has boilerplate code. Some more so than others but they all have their typing taxes. Whether it's the hundredth time you've typed from X import Y or the zillionth time you've written a getter & setter pair, it's time to look into a Vim boilerplating tool.

SnipMate comes highly recommended. It's simple to use, fully-featured, and pure-Vim portable.

The idea is straightforward - you set up keywords that tab-complete into finished code.

Installation

As always, make sure you've got Pathogen installed, then download/clone this repository to ~/.vim/bundle/snipMate.

Example

Here's a simple example. Once installed, put this in a ~/.vim/snippets/javascript.snippet file:

snippet log
    console.log(${1});

Now when you're editing a .js file, typing log<TAB> will expand into console.log();, and put the cursor in between the brackets. You're still in insert mode, so you just carry on typing your log statement.

There's More

SnipMate really is that simple to get started, and comes with a library of predefined snippets for many popular language, so it's useful as soon as you install it. But it has a couple of extra features that are worth highlighting.

First, you can define multiple, named placeholders. Consider a a snippet like this for Google Analytics event tracking:

snippet gaevent
    _gaq.push(['_trackEvent', '${1}', '${2}', '${3}', ${4}]);

Typing gaevent<TAB> would expand to the correct Analytics code. SnipMate knows there are 4 points in the boilerplate that need filling in, so it will put the cursor on the first immediately, still in insert mode, and tab will take you to each subsequent point.

But we can take this example a little further. To aid your memory, each variable can be given a placeholding name:

snippet gaevent
    _gaq.push([
        '_trackEvent',
        '${1:category}',
        '${2:action}',
        '${3:label}',
        ${4:value}
    ]);

Now typing gaevent<TAB> gives you this:

_gaq.push(['_trackEvent', 'category', 'action', 'label', value]);

Each insertion-point gets a temporary name. Tabbing between them now puts you in replace mode, so you hit tab and then keep typing to overwrite the name with your real value.

With those extras, SnipMate not only saves you a lot of typing, but also takes a huge burden-of-memory away from you, especially for APIs you don't use every day. And because snippets are trivial to write, they become quite habit-forming.

Polyglot

Another huge benefit of SnipMate comes if you're writing in multiple languages. Because SnipMate is FileType-aware, the same keyword can have different meanings for different languages. So log<TAB> can expand to a print in Python, a console.log in JavaScript, a logger.debug in Java, and so on. You can form the habit of typing a single word that just works correctly for the programming context you happen to be in. More mental energy saved.

There's plenty more to like about SnipMate, but since the aim is to get you started, we'll stop there. Take a look at :help snipMate.txt, or just browse the default snippets it comes with to learn more.

Okay, One Last Highlight

One last feature of SnipMate that's important - if it doesn't find a match when you hit tab, it just inserts a tab. Sounds simple, but the tab-completion libraries that get this wrong & try to interfere with every tab you type can drive you insane.

Download

Comments...

Written by