One of my projects this winter break has been to finally take the leap and switch to vim for all my text editing needs. The full story behind this momentous decision will have to wait for another day, but the short version is simply that, as my typing has improved over the past couple of months, I’ve finally started to notice how much reaching for the mouse slows me down. My goal in learning vim is to spend as much time as possible with my fingers on the home row. The learning curve has been steep indeed, but I’m finally at the point where I’m more productive in vim than I used to be in Sublime Text. As of earlier today, however, one problem remained: I didn’t have a good setup for LaTeX and vim. Back in 2013 I learned about the fantastic LaTeXTools plugin for Sublime which I’ve been using ever since. My main precondition for switching to vim has been to find a setup with comparable functionality.

After considering several alternatives, including LaTeX-Box and ATP, I finally decided to go with the venerable Vim-LaTeX plugin. Up until today, however, there were two deal-breakers:

  1. I couldn’t figure out how to set things up so that Vim-LaTeX would use latexmk to compile a pdf by running pdflatex the appropriate number of times (as is done by default in LaTeXTools) and,
  2. I couldn’t get forward or reverse search working with SyncTeX.

After a whole day of Googling and tinkering, I’m 95% satisfied with my solution, so here it is. You can view and fork all of the relevant configuration files in my dotfiles repository on GitHub. I keep a copy of the scripts from Step 2 in my bin repository.

Part I: Compilation Commands

First, make sure that you have all of the lines that a required for using vim-latex in your .vimrc file:

filetype plugin 
set grepprg=grep\ -nH\ $*

I also include the following optional, but sensible, suggestions:

filetype indent on
let g:tex_flavor='latex'

Now add these lines to your here’s the important part - add the following to your .vimrc file

let g:Tex_MultipleCompileFormats = 'pdf'
let g:Tex_DefaultTargetFormat='pdf'
let g:Tex_CompileRule_pdf = "latexmk -pdflatex='pdflatex -file-line-error -synctex=1 -interaction=nonstopmode' -bibtex -pdf $*"

After restarting vim and using \ll to compile your .tex file, you should have a pdf file generated by latexmk along with various auxiliary files including a synctex file. We’ll need this in the second step.

Part II: Evince and SyncTeX

I knew that forward and reverse search were possible using the (default) evince pdf viewer on Ubuntu 14.04 because both worked perfectly on my system with LaTeXTools and Sublime. Inside the evince directory in the GitHub Repo for LaTeXTools, I found three files that seemed to match those linked to on this thread on the Ubuntu forums. After downloading both sets of file, I diffed each pair. By doing this I learned that the evince_backward_search and evince_forward_search python scripts from LaTeX tools are slightly altered versions of the files from the Ubuntu forum that incorporate some of the more helpful suggestions from later in the thread. The evince_sync file, on the other hand, is a modified version of the evince script from the Ubuntu forum that has been changed to call Sublime rather than gvim for reverse search and to source both python scripts without assuming that they have been set to be executable. Since I knew they already worked on my system, I decided to use the evince_forward_search and evince_backward_search scripts from LaTeXTools and combine them with the evince script from the Ubuntu forum.

Here are the steps I followed:

  1. Copy the evince file from the Ubuntu forum thread and the evince_backward_search and evince_forward_search into ~/bin. If you’re on Ubuntu, you may need to create ~/bin first. If you find yourself in this situation, restart your machine before proceeding and ~/bin will automatically be added to your search path.
  2. Make each of the three scripts executable with chmod +x [filename]
  3. Add the following lines to ~/.vim/after/ftplugin/tex.vim (if this file doesn’t exist on your machine, create it first)
function! Tex_ForwardSearchLaTeX()
  let cmd = 'evince_forward_search ' . fnamemodify(Tex_GetMainFileName(), ":p:r") .  '.pdf ' . line(".") . ' ' . expand("%:p")
  let output = system(cmd)

And you’re done! Everything works! Well, almost everything works. As intended, \ls does a forward search and control-click inside of evince does a reverse search. The weird thing is that the first time I do a reverse search within a given pdf, evince launches two duplicate copies of gvim. All subsequent reverse searches work exactly as expected. I have absolutely no idea why this is, so if you have any thoughts, send me a tweet or email. In the meantime, my new LaTeX setup is good enough for me to get down to work!