Neovim, LaTeX and Zathura in perfect harmony

May 23, 2020 18:09 · 633 words · 3 minute read neovim LaTeX zathura vimscript


Zathura is my pdf viewer of choice: it is minimalist and has vim key bindings by default. I use it a lot when I’m writing TeX files in Neovim, partly as you can open it without leaving Neovim. And when you recompile the TeX file (I tend to use a makefile for this which I run from within vim) then it updates on the fly. As I’m using the i3 window tiling manager as part of Regolith OS this results in my screen reconfiguring itself just how I want it:

Neovim and Zathura side by side

Although calling Zathura isn’t a big deal from inside Neovim (:!zathura mydocument.pdf &), I thought I’d shave off a couple of seconds each time and reduce this to a key press by taking half a day to write my first ever vimscript. You may say false economy, I say a good way to procrastinate when you have a deadline due.

So it only takes me a quarter of a day if I decide to do something like this again, here are the steps. For reference I’m using Ubuntu 18.04.4 LTS on the Regolith desktop environment, TeX 3.14159265, Neovim v0.4.3 and Zathura 0.3.8 (with the plugin pdf-poppler 0.2.8).

Create a ftplugin file for TeX files

At the terminal type:

nvim ~/.local/share/nvim/site/ftplugin/tex.vim

This creates a file which Neovim will read when, and only when, you are editing TeX files; there’s no reason you can’t just place the code that follows in your init.vim / .vimrc, but I think this approach is neater. This also assumes that your folder structure follows mine, which it probably should do if you are using Linux and haven’t messed with it too much.

I’m led to believe if you are using plain vim rather than neovim this should be:

vim ~/.vim/ftplugin/tex.vim

Add the vimscript

Copy-paste the following vimscript into the file you’ve just created:

function! ZathuraOpenPdf()
	let fullPath = expand("%:p")
	let pdfFile = substitute(fullPath, ".tex", ".pdf", "")
	execute "silent !zathura '" . pdfFile . "' &"

nnoremap <A-p> :call ZathuraOpenPdf()<CR>

This finds the name of the current file from the buffer, chops the “.tex” off the end, replaces it with “.pdf” and then runs Zathura in a new window whenever you press the <Alt-p> key combination. The Alt key apparently maps funnily on some terminals, so you might want to alter this.


You might notice from the screenshot I’m using a linter. This is the ALE (Asynchronous Lint Engine) plugin which features TeX support out of the box. I really like ALE; though for LaTeX I don’t think a linter is essential, but it is nice to have.


This was my first time trying vimscript and given how notorious vim’s learning curve is, there are bound to be better / neater / more formally correct ways to have written this - feel free to comment below if this is the case.

Added bonus - Makefile

In case you are interested below are the contents of my Makefile, where mydocument.tex is the name of the relevant TeX document (the biber line refers to the bibliography software, if you aren’t using this it can be just shortened to one turn of pdflatex):

default: build

	pdflatex mydocument.tex
	biber mydocument
	pdflatex mydocument.tex
	pdflatex mydocument.tex

	-rm *.aux 
	-rm *.log 
	-rm *.lof 
	-rm *.bbl 
	-rm *.blg 
	-rm *.lot 
	-rm *.out 
	-rm *.toc 
	-rm *.bcf 
	-rm *.run.xml 
	-rm *.blx.bib 

If this is copy-pasted into a file called Makefile, then running :!make in Neovim will update your pdf, and in turn Zathura. Actually you don’t need the ! and can just use :make but for some reason this doesn’t return me back to the TeX file when I’ve finished, so I just insert the bang.

Further reference