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.
6 comments so far, add yours ▶