Introduction
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:
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 curFile = substitute(bufname("%"), ".tex", ".pdf", "")
execute "silent !zathura " curFile "&"
endfunction
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 key combination. The Alt key apparently maps funnily on some terminals, so you might want to alter this.
Linter
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.
Conclusion
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
build:
pdflatex mydocument.tex
biber mydocument
pdflatex mydocument.tex
pdflatex mydocument.tex
clean:
-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.
Top comments (0)