How to setup synctex with vim, pdflatex and an open-source PDF-viewer under Linux?

It would help to know which, if any, LaTeX plugin you're using for vim. (E.g., the latex-suite, vim-auctex, latex-box, etc.)

Next, as far as viewer choice, the only widely used open source PDF viewer for Linux which currently supports SyncTeX well out of the box is Okular. That's probably your best choice.

There are instructions fo setting up SyncTeX with Okular with the vim-latex plugin here, and some related observations here. I had mixed results following those directions. Here's what seems to be important.

  1. Be sure that your LaTeX compilation method (which will depend on your plugin) calls pdflatex (or xelatex or whatever) with the -synctex=1 flag.

  2. I think something like this should suffice for Forward Searches with Okular, though it might be better to try to rewrite or modify the forward search function for your plugin (there's some info on that in one of the links above). Put this in your .vimrc (and change the mapping to whatever you like).

    function! SyncTexForward()
         let execstr = "silent !okular --unique %:p:r.pdf\\#src:".line(".")."%:p &"
         exec execstr
    endfunction
    nmap <Leader>f :call SyncTexForward()<CR>
    
  3. For reverse searches, set the editor line in Okular to gvim --servername GVIM --remote +%l %f. It might also work to use gvim --servername GVIM --remote-send "<Esc>%lgg" if you only use it with the file already open. Change the servername to whatever you use. (Not sure if it's different with regular vim, but this doesn't make much sense out of a graphical environment.)

I do not have Okular installed right now, however, so I could not test any of that. (And the links I gave earlier have slightly different advice which is worth trying.) I really hope someone with both Okular and gvim installed can test this advice, and correct where I went wrong.

And all of that advice isn't going to work well if you're using subdocuments called through \input{...} or \include{...}, where the PDF name doesn't match the name of the document you're editing. There are ways around that, but it would require knowing more about what LaTeX plugins and methods you're already using, if any.

However, other choices are kinda/sorta already available. The next version of evince will support SyncTeX through D-Bus, and apparently someone is already working on a plugin for vim to make use of it. Details here. However, it's very unlikely that this version of evince is already available for your Linux distribution, and there may be some problems with it.

There's an old fork of an old version evince that provides synctex support; there are instructions that come with that detail how to set this up with gvim. It works fine. It's easy to set up if you're using Arch Linux, since this is in the AUR. If you're not, I don't know how hard it would be to compile. (I used to use Ubuntu before Arch, and couldn't get it working there, but that may have been my ignorance.)

Next, I wrote some scripts that provide very limited, very poor, but still better than nothing, synctex support between gvim and the open-source vim-like PDF viewer Zathura, which uses vim-like keybindings. You'll find them mentioned and detailed in this thread in the Arch forums here. (post #370)

Finally, I think this kind of stuff will work its way into the major LaTeX plugins for vim soon, and then you don't have to resort to so much trial and error.


This is a solution for evince, thanks to José Aliste who wrote gedit-synctex-plugin:

Preamble

  1. Download these files
  2. deflate them to ~/bin (or something within $PATH)

Backward Search (Evince → Editor)

  1. Adopt the first line of »~/bin/evince« (EDITORCMD) to your needs. (run »evince_backward_search« to get help for possible entries)
  2. Compile your .tex File with synctex (»pdflatex -synctex=1 myfile.tex«)
  3. Run »evince myfile.pdf« (The script should run evince_backward_search and evince)
  4. click on some text in evince with »Ctrl+leftclick«
  5. the editor should jump to the corresponding line

Forward Search (Editor → Evince)

  1. you have to tell your editor, to run »evince_forward_search $PDFFILE $LINE $TEXFILE« when pressing some key.
  2. go to some line in your editor and press the key
  3. evince should mark the corresponding line

Using vim-latexsuite, write in ~/.vim/ftplugin/tex.vim:

function! Tex_ForwardSearchLaTeX()
  let cmd = 'evince_forward_search ' . fnamemodify(Tex_GetMainFileName(), ":p:r") .  '.pdf ' . line(".") . ' ' . expand("%:p")
  let output = system(cmd)
endfunction

Afterwards you can do forward search in vim with \ls


Automatic LaTeX Plugin for Vim supports forward and backward search for many viewers (Xpdf, Okular, Evince, or Skim on MacOs).

Note: Link replaced by archive link. The software doesn't seem to exist anymore.