How to use reverse and forward search with vim and zathura


Light weight and with vim like key-sequence zathura is a great choice for reading pdf files. To install it simply run
 # apt-get install zathura
We will use pdflatex and synctex on foo.tex source file. In the tex source file we can add the following line:
alternatively we can compile foo.tex with the following option:
 $ pdflatex -synctex=1 foo.tex

Reverse Searching

First we need to edit zathurarc file, run
 $ vim $HOME/.config/zathura/zathurarc
and append the following lines
 # Adding reverse search between gvim and zathura
set synctex true
set synctex-editor-command "gvim --remote-silent +%{line} %{input}"
save and close the document.

Test your settings

  • Create a file foo.tex and compile it using pdflatex.
  • Open the new created foo.pdf document using zathura.
  • While reading foo.pdf hold the control key and left-click the mouse.
  • The source file should be opened in the very same line where you have clicked.

Forward Searching

We can jump to line 511 column 1 in the file foo.pdf by using the command line, simply run:
 $ zathura --synctex-forward 511:1:foo.tex foo.pdf
Nevertheless this is not what we want. What we really want is to find a way to do forward search from vim to zathura. To do this we edit vimrc file, run:
 $ vim $HOME/.vimrc 
and append the following text:

 function! SyncTexForward()
let linenumber=line(".")
let colnumber=col(".")
let filename=bufname("%")
let filenamePDF=filename[:-4]."pdf"
let execstr="!zathura --synctex-forward " . linenumber . ":" . colnumber . ":" . filename . " " . filenamePDF . "&>/dev/null &"
exec execstr 
nmap  :call SyncTexForward()

save and exit.

Test your settings

  • Run:
     $ vim foo.tex
  • Move the cursor to a chosen line.
  • Hold alt-s (that's alt and key character s at the same time).
  • The file foo.pdf should be opened and forward to your chosen line.