Sussman Lab

VimTeX

VimTeX modestly bills itself as “VimTeX: A modern Vim and neovim filetype plugin for LaTeX files.” There are certainly alternatives, but it might be the single most compelling reason that switching to (neo)vim as a LaTeX editor makes sense. It is well maintained an fantastically documented – there are multiple documentation pages, and there is a page of visualizations much better than what you are going to find below! I’ll again highlight the excellent guide to supercharged mathematical typesetting; since that exists I’ll be a bit briefer here than in other parts of this vim-and-latex guide. It also goes more deeply into actually setting up and configuring VimTeX, so if you want more granular instructions head on over there.

Compilation

First and foremost, VimTeX integrates extremely well out-of-the-box with several tools for compiling TeX (latexmk, tectonic, arara, etc). It is also easy to set it up with various pdf viewers. Different options are available depending on your OS – popular choices seem to be things like zathura on linux machines, SumatraPDF on Windows, Skim on MacOS, and I’ve found it straightforward to get good viewing, forward, and reverse searching up and running with all of them. For a single solution that will work will across platforms, I’m currently using Sioyek (even though the above screen recording is using SumatraPDF). Okular would be another good cross-platform choice.

As with everything else in VimTeX (so I will mostly stop repeating this), all of the options to control compilation and viewing are extremely configurable and well documented.

Forward and reverse searching

As mentioned above, it is straightforward to configure VimTeX so that it synchronizes nicely with various pdf viewers. At the moment I’m using Sioyek – it is configured out of the box to work well with VimTeX, and works on multiple operating systems. Here you can see the forward and reverse search functionality in action. VimTeX provides a command to do forward searching (VimtexView, with a default mapping of <localleader>lv), allowing you to quickly jump to the corresponding spot in the pdf. For reverse searching different pdf viewers need to be configured differently (all well explained in the VimTeX documentation linked to above in the vimtex-view-configuration section). Here I just need to activate synctex in my Sioyek session by hitting <F4>, and then a <ctrl>-<LMB> (control-click) performs the reverse search and jumps to the corresponding spot in the tex file. Reverse search even works well on projects spread across multiple files: you can configure whether you want a currently unopened tex file containing whatever you just reverse searched to open in the buffer, in a new split, etc.

The quickfix list

Compiling and viewing on its own is fine if you only ever write flawless LaTeX documents. For the rest of us, VimTeX integrates nicely into vim’s quickfix features. Using this, you can get all of your expected “where are my errors coming from” functionality you’ve come to expect from your LaTeX editor.

Citation and command completion

As I have done in the neovim configuration setup page, you can configure VimTeX to provide completion information to nvim-cmp (via the cmp-vimtex source). This allows you to use autocomplete features for many things: LaTeX commands, the names of packages (!), etc. Because of how nvim-cmp works, this even allows for fuzzy completions – very convenient if you’re prone to typos.

Perhaps more importantly, this autocompletion functionality also works for any included bibliography files. Below you can see an example of this. Sorry about the jumping around on the screen, by the way: for most of these screen recordings I’ve just set the font size to some absurd value, but you can see that here we get not only a completion menu of potential matches, but then if the screen is large enough a separate window of additional information for each citation. Visualizing all of that smoothly would have taken more time, and I think this gets the idea across.

It might be worth emphasizing that there really is a rich ecosystem of neovim plugins out there. Below is a visualization of an alternate way of (fuzzily) searching through your bibliography and inserting citations, using the bibtex extension to the telescope plugin.

Document organization

When working with a project contained in a single .tex file it is easy enough to keep the structure of the document in your head and just use either marks or direct searching to navigate through your document. As a project gets larger it is sometimes nice to have a more zoomed-out view of things, and VimTeX provides two useful features here.

Folds

Whether you use (neo)vim’s fold functionality is probably a strong matter of personal preference. If you do, though, VimTeX provides access to custom foldexpr-based folding. This nicely handles a LaTeX document’s preamble, sections, subsections, subsubsections, and various \begin{...}...\end{...} environments. You can also replace the fold text with a custom function, and I think it’s nice that these options are decoupled. You can activate this folding functionality by default every time you open a tex file; in the recording above I also show that you can manually activate it for individual files (I’m also just using constant za’s to toggle individual folds – you can of course do more sophisticated recursive folding and unfolding of nested folds).

Table of Contents

Navigating a large, multi-file project requires a different kind of zoomed-out view, and VimTeX provides a nice “Table Of Contents” function. One can quickly open up a table of contents that correctly handles include-ed and input files – here I’m showing it on a set of lecture notes in which each chapter has two different associated .tex files (one for the main text, the other for the list of problems at the end of the chapter) spread across multiple directories. In the table of contents you can configure (and modify on the fly) the depth of the displayed ToC – from \parts to \paragraphs to labeled equations and figures – and then instantly jump there. There is also a nice (if perhaps niche) built-in “To-do” functionality. In your document you can add comment strings like

%TODO: some additional text here

There is then a section of VimTeX’s ToC that lists all of your todos, allowing you to jump directly to them. Your mileage may vary, but it’s pretty cool.

New vim motions

Modal editing, vim motions, and text objects make up what I think of as the heart of the “language of vim.” VimTeX is not going to change anything about modal editing (unless, I suppose, you think of its integration with edit-compile-view-PDF tools as a quasi mode), but it does provide new motions and text objects for interacting with LaTeX documents. Here I’m just showing these off in the context of navigating through a document (because I changed my mind on what screen recordings I wanted after the fact, and didn’t go back and make more), but you can absolutely use these as first-class vim motions.

New text objects

VimTeX provides new text objects for interacting with LaTeX environments, or document sections, or math zones, or other commands. Now in addition to dip-ing and yap-ing and caw-ing you can da$ and yie and viP and whatnot.

New commands

VimTeX also provides some useful new commands – inspired by vim-surround, I suspect – that let you modify stuff surrounding an object of interest. This includes modifying environments, commands, and mathematical delimiters. The modification can involve deleting the surrounding stuff directly, or changing it (which queues a popup for you to type what you want to change the object to). There are also some fun “toggling” features, letting you quickly toggle between delimiter types, or the representation of fractions, or starred/unstarred versions of a command.

Insert mode features

Finally (for the purposes of this guide), VimTeX comes with its own set of math-environment-aware insert mode mappings, and especially if you are not going to be using a separate snippet engine these can be extremely helpful. They replicate the “autotriggered snippet” ability, letting you type a few characters and have it automatically expand out into various common LaTeX symbols and math commands. Here are a few examples; you can use the :VimtexImapList command to view all of the ones that come by default:

There are also some insert-mode functions of convenience, such as the ability to use ]] to automatically close the last open delimiter or environment:

If you aren’t going to write your own snippets you can add a few custom imaps to VimTeX (i.e., to leverage its context-aware functionality). Personally, I don’t love insert mode maps that depend on the timing of my keypresses, and I prefer to just handle all of this kind of functionality with LuaSnip. Fortunately, the VimTeX plugin is extremely configurable; for instance, turning off its imap feature is as simple as adding a line to the plugin configuration:

vim.g.vimtex_imaps_enabled = 0