Vim for LaTeX, part 4

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, install­ment in my ongo­ing series on con­fig­ur­ing Vim for use as a LaTeX editor.

One of my favorite plu­g­ins for Vim is snip­Mate. Snip­Mate is great for LaTeX, but it comes in handy any time I use Vim—be it for tex­tile, mark­down, emails in mutt, and so on.

The basic idea is sim­ple: snip­Mate gives you both default and cus­tom filetype-specific word or phrase com­ple­tions. So when I am writ­ing 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 ‘Sec­tion Name’ is high­lighted. If I start typ­ing it gets replaced with my title. Another <TAB> takes me to the sec­ond occurence of ‘Sec­tion Name’, and a third takes me to the blank space before the end of the sec­tion so I can start writing.

The default snip­pets are great and are really help­ful. But the real power comes in the fact that you can cus­tomize snip­pets really eas­ily. All you do is edit (or cre­ate) a filetype.snippets file for your file­type. An exam­ple will make this more clear.

I like to use the \citet{} and \citep{} com­mands for cita­tions fre­quently. But I don’t want to type that every time. So I put the fol­low­ing 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 cur­sor will be on the ‘pp’, and when I type a page num­ber it will write over that, a <TAB> will move the cur­sor to ‘key’, where I can write in my cite key, and a third <TAB> will move the cur­sor imme­di­ately after the clos­ing bracket.

In gen­eral, if you want to write a com­mand, you fol­low 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 high­lighted (so you can type over it) when you get to that tab stop, do a ${1:your text}.

You can do basi­cally any­thing you want here (except lead with a pound sign (#), since that is the com­ment leader for .snip­pets files). You could do a sig­na­ture or a para­graph or a long series of com­mands. If you want to set your LaTeX foot­notes off like this:

 end of line.%
%
%
\footnote{my note.}%
%
%

you can. The world is your oyster.

I have redone some com­mands (e.g., to do man­ual sec­tion folds on {{{ }}}), and have writ­ten an hand­ful of addi­tional snip­pets 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 snip­pets, you can find them one direc­tory up in that account

Note:

To be fair, I think I’m using an out­dated ver­sion of this plu­gin. Nev­er­the­less, it doesn’t work per­fectly for me. Some­times the tab stops get messed up and don’t put my cur­sor in the right place. It isn’t a big deal for me, but be warned that it might happen.

Even with the occa­sional prob­lem, I think snip­Mate makes my writ­ing life much eas­ier. I com­mend it to you, if you use Vim.

Posted Tuesday, August 31st, 2010 under LaTeX, text editors.

9 comments

  1. Thanks Char­lie, this looks really nice. It seems to be almost iden­ti­cal in func­tion to the snip­pets plu­gin for gedit; I gather that both are inspired by TextMate’s snip­pets fea­ture. (Or vice-versa or something.)

    As may have been obvi­ous, I’ve been suf­fer­ing from severe edi­tor inde­ci­sive­ness, try­ing every­thing under the sun. For the moment, how­ever, I think I’ve landed on vim. It’s a lot of fun, but I still stum­ble with it. I’m try­ing to learn things a bit at a time, and not try to mas­ter every­thing at once. (I’m also going full immer­sion, using vim­per­a­tor for web brows­ing, zathura/apvlv for PDF view­ing, etc.) So I used it for a cou­ple weeks sans plu­g­ins, and just recently started feel­ing con­fort­able with the core edi­tor, and so now I’m learn­ing the latex plu­g­ins and crib­bing together the parts I like from each. Prob­a­bly I’ll wait another cou­ple of weeks to adding the snip­pets plugins.

    Right now, I’m off to research if there’s a way to auto­mat­i­cally change col­orschemes when I switch in and out of insert mode, so it’s more obvi­ous. I’m sure you can. The damn thing can do anything.

    (A: yep, very simple:

    autocmd InsertEnter * colorscheme <scheme-for-insert-mode>
    autocmd InsertLeave * colorscheme <scheme-for-other-modes>
    

    vim rules.)

    • I’m glad to hear you are becom­ing a Vim con­vert. I try not to be too preachy about it, since I don’t really know any other edi­tors (besides Text­Mate and TeXShop, which I did use for a lit­tle while), but I have a hard time believ­ing any­thing could be bet­ter than Vim.

      For me, the big sell­ing point of Vim over any other edi­tor is the modal nature.

      And the plu­g­ins, though not unique to Vim, are also excel­lent. A hand­ful of the right plu­g­ins make writ­ing LaTeX so much eas­ier. You still have to know that you want \citeyearpar instead of \citep, but you don’t have to type the whole blasted thing every time.

      (I’m pretty sure snip­Mate is taken from TextMate–the name cer­tainly sug­gests it at least.) Re: other soft­ware with Vim modes. At home I use a Mac, and there is a nice Safari plu­gin that gets a hand­ful of the Vim fea­tures right (here is the github page). It isn’t per­fect, but some of the fea­tures are nice.

      At work I’m using my Arch Linux-powered lap­top, and I’m try­ing to get used to using luakit (<a href=“http://github.com/mason-larobina/luakit>github). It is a lit­tle slower than I’d like for some rea­son (though I also have tried uzbl, which is faster and is the same basic idea, though not writ­ten in lua), but I think it is truer to Vim than the Safari exten­sion, at least at this point.

  2. I think resistence to vim is just its steep learn­ing curve com­bined with… some­one on the arch forums put it this way: “it makes you incom­pat­i­ble with every­one else.” Vim is about as far as you can get from liv­ing up to IEC/ISO stan­dards of con­si­tency among user inter­faces, and in gen­eral, con­sis­tency and cross-application stan­dards com­pli­ance is a good thing.

    I did try uzbl briefly, but found it buggy. There are quite a few sim­i­lar projects: vim­prob­a­ble, jumanji, dwb, but none quite did the trick for me. Since vim­per­a­tor is just a plu­gin for Fire­fox, you get a sta­ble, stan­dards com­pli­ant browser (if not the fastest).

    What I par­tic­u­lar like about it, how­ever, is that you can hit Ctrl-i in any text box in any web­page, and it will open vim with any con­tents of the box (assum­ing 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 con­fig­ure it to con­fig­ure vim for dif­fer­ent file­types . E.g., I have textboxes at char​li​etanksley​.net set as html since these com­ments use html mark-up.

    Google Chromium/Chrome has a plu­gin called Vim­ium which is some­what sim­i­lar, though it doesn’t seem to work exactly right with the newer ver­sions of Chromium. I’ve heard there’s one for Opera too.

  3. That’s a nice fea­ture, but it seems labo­ri­ous to get all those snip­pets set up. I sup­pose there’s some kind of library of stan­dard LaTeX snip­pets that you can just dump into the rel­e­vant con­fig­u­ra­tion file…

    Emacs with Auc­TeX has com­mands to inter­ac­tively insert sec­tions and envi­ron­ments by default. And with ref­tex, you can inter­ac­tively insert a vari­ety of cita­tion com­mands and search your bib­li­og­ra­phy keys for the right one. I imag­ine there is a sim­i­larly pow­er­ful plug in for vim…

  4. Hi Sea­mus,

    It isn’t quite so labo­ri­ous as it might sound. When you install the plu­gin, loads of snip­pet files are included by default. That includes snip­pets for your list envi­ron­ments, sec­tions and sub­sec­tions, etc.

    But you can write exten­sions pretty eas­ily (at least, I don’t mind mak­ing changes to the right text file). And I think that is nice.

    Also, the Latex-box plu­gin I wrote about a few weeks ago includes some of the Auc­TeX func­tion­al­ity you men­tion, though it doesn’t aim to do as much as AucTeX.

  5. The vim LaTeX suite plu­gin cer­tainly has fea­tures like this, as does, as you might have guessed, the “vim-AUCTeX” plu­gin which tries to emu­late AUC­TeX in vim, but per­son­ally, I like hav­ing to set things up myself, because then I can make it work exactly the way I want. The way that the LaTeX suite plu­gin, for exam­ple, han­dled this kind of thing drove me nuts, and it wasn’t obvi­ous how to tweak it. I also use cus­tom envi­ron­ments and com­mands at least as much as the pre­de­fined ones, and nat­u­rally those aren’t going to come in a list of pre-defined snip­pets. The latex-box plu­gin also has some generic “wrap high­lighted sec­tion” in what­ever envi­ron­ment or com­mand you want fill in next, etc.

  6. Nice, thanks for the fast answer :)

Leave a Reply