(Neo)vim as a LaTeX editor
They say that the best camera is the one that makes you want to go out and take pictures.
Writing documents in LaTeX is a major part of my job, and for a long time I just used some of the standard TeX editors – TeXnicCenter, TeXShop, TeXstudio,TeXworks, Overleaf, and the like. I was always jumping between Windows, Mac, and Linux machines, and since their cross-platform support was usually poor (and I never liked writing TeX in my browser) I never really bothered to learn how to use any of these editors effectively. I saw someone use BaKoMa once and thought it was cool, but as a grad student was put off by the price. I then somehow went many years just using the vanilla version of whatever TeX editor happened to be on my computer at the time.
In parallel, another major part of my job is writing scientific code. Since I was always jumping between my local computer and various remote servers, I spent years learning Vim. At the risk of sounding ridiculous, Vim is just… fun. Yes, I find it a bit faster than working in a graphical keyboard-and-mouse text editor, but that’s not really the point. There is a kind of “speaking another language” feeling – dip
-ing and yap
-ing and caw
-ing – while writing with Vim. Maybe it’s just me, but it tickles the brain.
Well, one day I happened to come across this remarkable blog post by Gilles Castel. Somehow, it had never even occurred to me that there would – of course – be a thriving, open-source ecosystem that would make writing LaTex easy (including all of the compile, open the pdf, and forward/inverse search functionality I’d come to expect from my other TeX editors), fun (because it was in Vim), and radically faster and less tedious. There are already several excellent guides to getting vim or neovim set up for efficient LaTeX document creation – I’d like to particularly recommend Elijan Mastnak’s guide to supercharged mathematical typesetting, and if all you get out of this page is a link to that guide I’ll be happy.
I think one of the best ways to learn is to try to teach, though, so as I was learning about the Vim-LaTeX ecosystem I took notes and tried to write in a vaguely pedagogical manner. I separated the notes out into four parts, and below are links related to (1) setting up neovim, (2) writing TeX-focused snippets, (3) using the excellent VimTex plugin, and (4) playing around with the TeXpresso project. The end result of this can be seen in the demonstration video at the top of this page – similar motivational videos decorate the guides below – hope you enjoy learning about all of this as much as I did!
Guides
Setting up neovim
Out of the box neovim – a fork of vim – feels exactly like someone took vanilla vim and set a few sensible default options. After playing around for a week, though, I found neovim much easier to configure and integrate with plugins, and I think I’ll stick with it moving forward. The ecosystem around it is great and the community seems very positive . Also, it makes use of Lua instead of vimscript.
There are already several very good resources to either get started from scratch with neovim or migrate from vim. If you want my take, or want to quickly reference some of my settings that I use in other parts of this series, read more here!
Working with snippets
LaTeX is amazing, but it also involves writing a lot of boilerplate markup. Wouldn’t it be nice to be able to type “fig” and optionally have it expand to a full \begin{figure}...\end{figure}
, complete with all of your favorite \centering
and \includegraphics[width=0.9\linewidth]{filename}
and… default options? What about making the typing experience mirror the way you speak mathematical equations, like having curl nhat
automatically (but only in math mode) expand out into \nabla\times\hat{n}
?
Writing your own snippets makes this kind of functionality straightforward, and it is easy to connect neovim with a powerful snippet engine (“LuaSnip”). My favorite guide is EJ Mastnak’s, with Evelyn Koo getting into some more advanced concepts. If you want my take, read more here!
The VimTeX plugin
Standard LaTeX editors make it easy to integrate the pdf viewing experience with writing the actual .tex
file. This includes not only compiling and displaying the compiled document, but being able to forward and inverse search (i.e., jumping from somewhere in your tex file to the corresponding spot in the pdf, and vice versa). The outstanding VimTeX plugin does this, and if it had stopped there it would have been enough. But on top of all of that, it also:
- Adds TeX-specific vim motions and text objects
- Allows autocompletion of commands, citations, labels, filenames…
- Adds surround-like commands to change, delete, toggle, and otherwise modify surrounding commands and environments
- Supports syntax highlighting, indenting, and vim folding
- Supports document navigation, table of contents, and “to-do” functionality
To top all of this off, VimTeX is extremely well written and maintained. Honestly, you could just go it its documentation page to get everything set up. If you want my take, read more here!
Live TeX rendering with TeXpresso
The TeXpresso project provides live rendering and error reporting for TeX documents (with fun add-ons like having the color scheme of the pdf renderer synchronize with your colorscheme in neovim). I mostly wanted to add this as just one example of the “thriving, open-source ecosystem” I mentioned above. There aren’t a lot of resources out there referencing this project, and I made at least one small change that I think improves the TeXpresso-in-neovim-in-WSL experience. If you’re interested, read more here!
How the screen recordings were made
At the top of the page and throughout these guides I’ve included some screen recordings to better demonstrate various concepts or features. I even left in all of the typos so you know all of this functionality isn’t just post-production magic (and wow – I am much worse at typing when I think someone is watching, apparently).
In case you’re curious, making them was pretty straightforward – especially since I haven’t optimized for quality of presentation. The screen capture itself just used the default Windows 11 snipping tool, with the resulting mp4 compressed using WSL and an ffmpeg command along the lines of:
ffmpeg -i input.mp4 -vcodec libx264 -crf 18 output.mp4
Within neovim the keys being pressed are shown using the Screenkey plugin. Occasional popup messages about available keybindings and VimTeX compilation are courtesy of the “clue” and “notify” mini plugins.
A few recordings were made instead on the Mac laptop I used to do zoom lectures on – those used Camtasia and hence have cheesy effects.
Everything works across Windows, Mac, and Linux systems, except for TeXpresso project. That I have sitting inside Ubuntu-24.04 on WSL2. And yes, I wish that I knew how to make tiling window managers work with applications launched from WSL.