With the advent of a new school year (at a new school), I’ve fallen away from the PhilTeX blog and forum much more than I would’ve liked. But I’m back for another, much shorter, installment in my ongoing series on configuring Vim for use as a LaTeX editor.
One of my favorite plugins for Vim is snipMate. SnipMate is great for LaTeX, but it comes in handy any time I use Vim—be it for textile, markdown, emails in mutt, and so on.
The basic idea is simple: snipMate gives you both default and custom filetype-specific word or phrase completions. So when I am writing in a .tex file, if I type sec and then a TAB I get the following:
\section{Section Name} % {{{
\label{sec:Section Name}
% section Section Name }}}
What is more, the first occurence of ‘Section Name’ is highlighted. If I start typing it gets replaced with my title. Another <TAB> takes me to the second occurence of ‘Section Name’, and a third takes me to the blank space before the end of the section so I can start writing.
The default snippets are great and are really helpful. But the real power comes in the fact that you can customize snippets really easily. All you do is edit (or create) a filetype.snippets file for your filetype. An example will make this more clear.
I like to use the \citet{} and \citep{} commands for citations frequently. But I don’t want to type that every time. So I put the following in my tex.snippets file
# citations
snippet cpp
\citep[${1:pp}]{${2:key}}${3}
snippet cp
\citep{${1}}${2}
snippet ctt
\citet[${1:pp}]{${2:key}}${3}
snippet ct
\citet{${1}}${2}
This means that cpp<TAB> will give me \citep[pp]{key}. The cursor will be on the ‘pp’, and when I type a page number it will write over that, a <TAB> will move the cursor to ‘key’, where I can write in my cite key, and a third <TAB> will move the cursor immediately after the closing bracket.
In general, if you want to write a command, you follow this formula:
snippet shortcut_key
\command_you_want{${1}}
You can have as many tab stops as you want, so far as I can tell: just put ${1} for the first, and so on. If you want some text to be highlighted (so you can type over it) when you get to that tab stop, do a ${1:your text}.
You can do basically anything you want here (except lead with a pound sign (#), since that is the comment leader for .snippets files). You could do a signature or a paragraph or a long series of commands. If you want to set your LaTeX footnotes off like this:
end of line.%
%
%
\footnote{my note.}%
%
%
you can. The world is your oyster.
I have redone some commands (e.g., to do manual section folds on {{{ }}}), and have written an handful of additional snippets for my .tex files. Feel free to check them out on this page of my github account. If you want to look at all my snippets, you can find them one directory up in that account
Note:
To be fair, I think I’m using an outdated version of this plugin. Nevertheless, it doesn’t work perfectly for me. Sometimes the tab stops get messed up and don’t put my cursor in the right place. It isn’t a big deal for me, but be warned that it might happen.
Even with the occasional problem, I think snipMate makes my writing life much easier. I commend it to you, if you use Vim.

Thanks Charlie, this looks really nice. It seems to be almost identical in function to the snippets plugin for gedit; I gather that both are inspired by TextMate’s snippets feature. (Or vice-versa or something.)
As may have been obvious, I’ve been suffering from severe editor indecisiveness, trying everything under the sun. For the moment, however, I think I’ve landed on vim. It’s a lot of fun, but I still stumble with it. I’m trying to learn things a bit at a time, and not try to master everything at once. (I’m also going full immersion, using vimperator for web browsing, zathura/apvlv for PDF viewing, etc.) So I used it for a couple weeks sans plugins, and just recently started feeling confortable with the core editor, and so now I’m learning the latex plugins and cribbing together the parts I like from each. Probably I’ll wait another couple of weeks to adding the snippets plugins.
Right now, I’m off to research if there’s a way to automatically change colorschemes when I switch in and out of insert mode, so it’s more obvious. I’m sure you can. The damn thing can do anything.
(A: yep, very simple:
vim rules.)
I’m glad to hear you are becoming a Vim convert. I try not to be too preachy about it, since I don’t really know any other editors (besides TextMate and TeXShop, which I did use for a little while), but I have a hard time believing anything could be better than Vim.
For me, the big selling point of Vim over any other editor is the modal nature.
And the plugins, though not unique to Vim, are also excellent. A handful of the right plugins make writing LaTeX so much easier. You still have to know that you want
\citeyearparinstead of\citep, but you don’t have to type the whole blasted thing every time.(I’m pretty sure snipMate is taken from TextMate–the name certainly suggests it at least.) Re: other software with Vim modes. At home I use a Mac, and there is a nice Safari plugin that gets a handful of the Vim features right (here is the github page). It isn’t perfect, but some of the features are nice.
At work I’m using my Arch Linux-powered laptop, and I’m trying to get used to using luakit (<a href=“http://github.com/mason-larobina/luakit>github). It is a little slower than I’d like for some reason (though I also have tried uzbl, which is faster and is the same basic idea, though not written in lua), but I think it is truer to Vim than the Safari extension, at least at this point.
I think resistence to vim is just its steep learning curve combined with… someone on the arch forums put it this way: “it makes you incompatible with everyone else.” Vim is about as far as you can get from living up to IEC/ISO standards of consitency among user interfaces, and in general, consistency and cross-application standards compliance is a good thing.
I did try uzbl briefly, but found it buggy. There are quite a few similar projects: vimprobable, jumanji, dwb, but none quite did the trick for me. Since vimperator is just a plugin for Firefox, you get a stable, standards compliant browser (if not the fastest).
What I particular like about it, however, is that you can hit Ctrl-i in any text box in any webpage, and it will open vim with any contents of the box (assuming it’s not empty), let you fill out the textbox using vim, and then dump the buffer back in to the textbox when you’re done. You can even configure it to configure vim for different filetypes . E.g., I have textboxes at charlietanksley.net set as html since these comments use html mark-up.
Google Chromium/Chrome has a plugin called Vimium which is somewhat similar, though it doesn’t seem to work exactly right with the newer versions of Chromium. I’ve heard there’s one for Opera too.
That’s a nice feature, but it seems laborious to get all those snippets set up. I suppose there’s some kind of library of standard LaTeX snippets that you can just dump into the relevant configuration file…
Emacs with AucTeX has commands to interactively insert sections and environments by default. And with reftex, you can interactively insert a variety of citation commands and search your bibliography keys for the right one. I imagine there is a similarly powerful plug in for vim…
Hi Seamus,
It isn’t quite so laborious as it might sound. When you install the plugin, loads of snippet files are included by default. That includes snippets for your list environments, sections and subsections, etc.
But you can write extensions pretty easily (at least, I don’t mind making changes to the right text file). And I think that is nice.
Also, the Latex-box plugin I wrote about a few weeks ago includes some of the AucTeX functionality you mention, though it doesn’t aim to do as much as AucTeX.
The vim LaTeX suite plugin certainly has features like this, as does, as you might have guessed, the “vim-AUCTeX” plugin which tries to emulate AUCTeX in vim, but personally, I like having to set things up myself, because then I can make it work exactly the way I want. The way that the LaTeX suite plugin, for example, handled this kind of thing drove me nuts, and it wasn’t obvious how to tweak it. I also use custom environments and commands at least as much as the predefined ones, and naturally those aren’t going to come in a list of pre-defined snippets. The latex-box plugin also has some generic “wrap highlighted section” in whatever environment or command you want fill in next, etc.
I get a 404 at http://github.com/charlietanksley/dotfiles/blob/master/.vim/snippets/tex.snippets
Hi Keba, Sorry about that. I actually decided the proper thing to do is fork Snipmate, so my snippets are in a new location. You can find the relevant page at https://github.com/charlietanksley/snipmate.vim/blob/master/snippets/tex.snippets.
Nice, thanks for the fast answer