# ankursinha.in/blog

neuroscience/fedora/musings

Tue 19 September 2017

# Writing LaTeX well in Vim

Posted by ankur in Tech (921 words, approximately a 4 minute read)

Vim is a great text editor if one takes a bit of time to learn how to use it properly. There's quite enough documentation on how to use Vim correctly, and efficiently so I shan't cover that here. vimtutor is an excellent resource to begin at.

Similarly, LaTeX is a brilliant documentation system, especially for scientific writing if one takes the time to learn it. Unlike the usual Microsoft Word type systems, LaTeX is a set of commands/macros. Once the document is written using these, it must be compiled to produce a PDF document. It may appear daunting at first, but after one is familiar with it, it makes writing a breeze. Now, there are a editors especially designed for LaTeX, but given that I use Vim for about all my writing, I use it for LaTeX too.

On Fedora, you can install Vim using DNF: sudo dnf install vim-enhanced vim-X11. I install the X11 package too to use the system clipboard.

## LaTeX tools

To begin with, there are a few command line commands that one can use other than the necessary latex, pdflatex, bibtex, biber, and so on commands:

• latexmk is a great tool that figures out the compilation sequence required to generate the document, and it does it for you.
• lacheck and chktex are both linters for LaTeX that make writing a lot easier.
• detex strips a tex document of LaTeX commands to produce only the text bits.
• diction, and style give the author an idea of the readability of the text.

One can use any text editor and then these utilities to improve their LaTeX writing experience.

On Fedora, install these with DNF: sudo dnf install latexmk /usr/bin/lacheck /usr/bin/chktex /usr/bin/detex diction. (Yes, you can tell DNF what file you want to install too!)

## Built-in Vim features

Vim already contains quite a few features that make writing quite easy;

• Omni completion provides good suggestions based on the text under the cursor.
• There's in-built spell checking already.
• Folding logical bits makes the document easier to read and navigate through.
• Syntax highlighting makes it a lot easier to read code by marking different commands in different colours.
• There are different flavours of linenumbers that make moving about a document much simpler.
• At some point, the conceal feature was added that further improves readability of documents
• Buffers, tabs, windows are available in Vim too, of course.

## Vim plug-ins

There are a lot of Vim plug-ins that extend some functionality or the other. The simplest way to install plug-ins is to use Vundle. Here are some plug-ins that I use. They're not all specific to LaTeX.

• Fastfold makes folding faster.
• vim-polyglot provides better syntax highlighting for a many languages.
• vim-airline provides an excellent, informative status line.
• tagbar lists sections (tags in general) in a different pane.
• vim-colors-solarized provides the solarized themes for Vim.
• vimtex provides commands to quickly compile LaTeX files, complete references, citations, navigate quicker, view the generated files, and so on.
• ultisnips provides lots of snippets for many languages, including LaTeX. Get the snippets from the vim-snippets plug-in.
• YouCompleteMe is a completion engine that supports many languages. Remember that this one needs to be compiled!
• Syntastic provides syntax checkers for many languages, including LaTeX.

I've also used vim-latex in the past and it's very very good. However, since I have other plug-ins that provide the various functionality that it brings together for many other languages too, I'm no longer using it. Worth a go, though.

## An example document

The image below shows a LaTeX file open in Vim with different plug-ins in action:

• On top, one can see the open buffer. Only one buffer is open at the moment.
• In the left hand side margin, one can see the fold indicators.
• The S> bit is an indicator from the linter that Syntastic uses, showing a warning or an error.
• The line numbers are also visible in the left margin. Since I am in insert mode, they're just plain line numbers. Once one leaves insert mode, they change to relative.
• On line 171, the conceal feature shows Greek symbols instead of their LaTeX commands.
• Syntax highlighting is clearly visible. The commands have different colours. This is the solarized dark theme, of course.
• The "pop-up" shows Ultisnips at work. Here, I'm looking at adding a new equation environment.
• Underneath the pop up, the dashed line is a folded section. The + symbol in the left margin implies that it is folded.
• In the status line, one can see that spell check is enabled, and that I'm using the en_gb language.
• Next, the git status, and the git branch I'm in. That's the vim-fugitive plug-in at work.
• Then, the filetype, the encoding, the number of words and so on provided by the airline plug-in.

Neat, huh? There is a lot more there that isn't easy to show in a screen-shot. For example, \ll will compile the LaTeX file; \lv opens the generated PDF file in a PDF viewer, Evince in my case; \lc will clean the directory of any temporary files that were generated while compiling the document.

I keep all my vimfiles on Github. Feel free to take a look and derive your own. I tweak my configuration each time I find something new, though, so it may change rather frequently. Remember to read the documentation for whatever plug-ins in use. They provide a lot of options, lots of shortcuts, lots of other commands, and sometimes setting them up incorrectly can cause vim to behave in unexpected ways.

TL;DR: Use Vim, and use LaTeX!!