Taming Vim — 2. Plugins
This is the second part in the series Taming Vim, a series focused on improving the intermediate Vim user’s understanding and operation of the text editor.
Introduction
Vim features an amazing amount of functionality at its core, functionality built up over the years (Vim itself is 20 years old this year and Vi, in the incarnation we know today, was created more than 30 years ago!) The beauty of Vim’s design is the “language” (see Jim Dennis’ exquisite answer linked from my first post) used to communicate your intentions to the editor.
For example, by introducing only a new motion you can now select, compose or otherwise operate on a new “shape” 1 of text with the same verbs you’re already familiar with.
There are hundreds of official plugins for Vim, and probably hundreds more unofficial plugins, these are some of the plugins I use and am familiar with with, it is by no means a complete list. I encourage you to explore the Vim plugins available through official and unofficial channels.
Plugins
I recommend using Pathogen to automatically load your plugins while keeping
them in separate directories for ease of management, it has the added bonus of
being able to generate help tags for all plugins with the :Helptags command.
indent-object
Vim text objects provide a convenient way to select and operate
on the shape of text. In a language like Python it can be useful to operate
on text in your current level of indentation, enter indent-object. For example,
assume that ▯ is the cursor in normal mode:
if cond:
foo()
for x in xs:
▯bar(x)
From this line pressing <ai will unindent the entire loop, including the
for line by one level. From the same line pressing <ii will unindent only
the loop body.
:h indent-object
Tabular
Simply put, Tabular aligns a body of text on a pattern; for example, lining up your assignment statements, which is a grueling task when done by hand. Drew Neil covers this plugin in one of his excellent screencasts, well worth watching.
:h tabular
surround.vim
This powerful plugin is all about things that surround other things:
parentheses, brackets, quotes, XML tags, etc. For example, assume again that
▯ is the cursor in normal mode:
Hello ▯world.
Pressing yss" will wrap the whole line (you can use any motion instead of
the final s here), excluding leading whitespace, in double-quotes:
"Hello ▯world."
Pressing cs"' will change the surrounding double-quotes to single-quotes:
'Hello ▯world.'
Pressing cs') will change the surrounding single-quotes to parentheses
without padding (use ( for inner-padding):
(Hello ▯world.)
Finally, pressing ds) will delete the surrounding parentheses:
Hello ▯world.
This really just scratches the surface of surround.vim, something I didn’t cover is using it when writing an SGML-based language, like XML or HTML.
:h surround
Gundo
Vim’s undo feature may look like any normal linear undo history, u to undo
and Ctrl-r to redo. Actually it’s a tree! When you modify an
older text state, i.e. pressing u a few times and then changing the buffer,
you are creating a new undo branch. Pressing u or Ctrl-r moves you backward
or forward chronologically along your current branch, g+ and g-
traverse the entire tree in chronological order. You can even go back and
forward by a relative amount of time with :earlier and
:later:, use :earlier 5m to go back to a state from 5 minutes ago!
Time travel!
It can be tricky to visualize this undo tree, which is where Gundo comes in, it draws a pretty ASCII tree showing you the current undo tree and will let you move around it easily.
Drew Neil explains it with pretty diagrams and Back to the Future references.
:h gundo.txt
LustyExplorer
Managing buffers can be an arduous process, inevitably when working on a large project you’ll either have a million hidden buffers or a million tabs open (and probably still have hidden buffers.) LustyExplorer can search your buffers by name (with fuzzy matching) and can grep the contents of your buffers to find that other buffer with that text that said that thing in it. Hello productivity!
As well as being a buffer manager, LustyExplorer is a great filesystem explorer
that’ll let you search your filesystem, again with fuzzy matching, from either
the current working directory or the directory of the current file. Once you’ve
narrowed your search down you can press <Enter> or <Tab> to open the file
in the current window, or Ctrl-t, Ctrl-o, Ctrl-v to open the file in a
new tab, horizontal split or vertical split, respectively.
Unfortunately the only complete documentation exists as a comment within the plugin source itself.
snipMate
If you’ve ever used snippets in applications like TextMate or Visual Studio then you’ve used snipMate. With a comprehensive list of snippets for popular languages, using a snippet is as simple as typing the keyword and hitting tab:
cl<tab>
Produces:
class ClassName(object):
"""docstring for ClassName"""
def __init__(self, arg):
super(ClassName, self).__init__()
self.arg = arg
Pressing tab again moves your cursor between the snippet placeholders. You can define your own snippets too which is extremely useful for repetitive tasks such as laying out unit test skeletons.
:h snipMate.txt
Wild geese
Plugins are a great way to make big improvements with a little action but don’t rely on gimmicks for Real Ultimate Power, exploit Vim’s core strengths and rely on plugins to reinforce the weak areas. Don’t be the person whose Vim session has ten information-panels, a multi-line toolbar and only a 40x25 window for entering text.
In the next installment I’ll talk about keyboard mappings and touch on Vim macros.
Major Tom to ground control!
(Writing text is fun and imagining I’m helping people makes me feel warm and fuzzy but it leaves me feeling a little detached. I’ve decided to ask the Internets a question at the end of each post that can be answered in a tweet, both to educate me and other people. If you enjoyed this article or learned something I’d appreciate it if you participated, consider it a knowledge donation!)
Tweet me your one Vim plugin you’re most grateful for, especially if you wrote it yourself.
-
By “shape” I mean the external characteristics that separate, for example, words from sentences, sentences from paragraphs, individual words from quoted phrases, etc. Vim often refers to these as “objects” or “text objects” which I find a slightly dull term. ↩