TeX and LaTeX plugins for Vim

By John Lenz. April 5, 2013.

I used Vim-LaTeX for many years, but in the past year or so I have dropped Vim-LaTeX and replaced it with several single-purpose plugins. My main issues with the Vim-LaTeX plugin was the way it handled compilation and error messages, I didn't like the way it handled folding, I wanted more control over snippets, and there was quite a bit of the package I did not use. Don't get me wrong, Vim-LaTeX is a great plugin and I was happy with it for a long time, but just small little things led me to investigate alternatives. See also part 2 and part 3.

Managing Plugins

The best way of managing plugins is pathogen. I'm not a big git user, so I install and use the hg-git plugin for mercurial and then clone the various plugins using mercurial into the .vim/bundle directory. Also, instead of using subrepositories or guestrepo, I wrote a short shell script to initially clone and keep the plugins up to date. Maybe instead I could make all the plugins guestrepo's of my main academic mercurial repository, but the script works for me. I store this script in my main academic mercurial repository so it is easy to keep the list of plugins synced between computers.

Vim Configuration

I keep my main vim configuration inside my academic mercurial repository, so my entire ~/.vimrc is

set nocompatible
filetype off
set rtp+=~/.vim/bundle/vim-pathogen/
call pathogen#infect()

source ~/academic/tools/vimrc

filetype plugin indent on

and the first line of ~/academic/tools/vimrc is

set rtp+=~/academic/tools

Therefore, ~/academic/tools is on the runtime path so for example my TeX specific configuration is in the file ~/academic/tools/ftplugin/tex.vim, all tracked and synced by mercurial. My ~/.vim directory is empty except for the bundle directory for pathogen, but even that could be removed if I moved to guestrepos.

Some General Plugins

The general (non TeX-specific) plugins I use are nerdtree, bufexplorer, ctrlp, and lawrencium. In particular, ctrlp is awesome, especially the ability to jump to tags. When building (see below), I create tags from labels, so with ctrlp I can quickly jump to a lemma or theorem no matter which file it is in.


To build, I use the tool rubber. I wrote a generic makefile latex.mk, which I store at ~/academic/tools/latex.mk. Note several things about this makefile which you might need to edit: I have hard-coded the path to my references and also a hard-coded check that a file exists (to see if the repository is checked out properly).

Inside ~/academic/tools/ftplugin/tex.vim, I have the following:

setlocal errorformat=%f:%l:\ %m,%f:%l-%\\d%\\+:\ %m
if filereadable('Makefile')
    setlocal makeprg=make
    exec "setlocal makeprg=make\\ -f\\ ~/academic/tools/latex.mk\\ " . substitute(bufname("%"),"tex","pdf", "")

For small projects I don't write a custom makefile and so tex.vim sets makeprg to directly use latex.mk. For my larger projects, I create a makefile that looks something like the following (from this paper).

PAGES=Multicolor?Ramsey \

finite_field_ams_DEPS=construction.tex intro.tex ramsey.tex appendix.tex smallm.tex openprob.tex

.PHONY: all
all: finite-field-ams.pdf tags

include ../../tools/latex.mk

Either with or without the makefile, I can type ":make" within vim and the combination of rubber and the errorformat I set causes the errors to be pulled into the vim quickfix window, so can list errors, jump between errors, etc. using the normal vim quickfix commands.

Next Time

In the next post, I will show how I setup snippets and folding by markers from the snippets, forward searching to open the resulting PDF to a location matching the location in vim, and some other short custom vim code I have accumulated to help with LaTeX editing.