As an academic I am often writing LaTeX code for publications or general documentation. I used to use ShareLatex and Overleaf because I have a secret love of GUIs (Lord forgive me!), but recently my co-authors have prefered to work in LaTeX repos shared on GitHub. For this reason my most recent paper was actually written entirely in Vim! This post isn’t meant to be a complete description of the best way to edit LaTeX in Vim, but instead I want to share some of the tools, tricks, and tips that I’ve found useful when writing LaTeX in Vim.
Vim environment setup
I’ve made a nice little GitHub repo to host my Vim configuration settings (.vimrc) so that porting it between my machines is made easier. No need to copy my .vimrc wholesale if you don’t like everything, but these are a few options that I use:
Installing Vundle for Vim package management and sensible.vim for near-universal improvements to the default Vim settings.
Tabs vs spaces… We could fight all day about using tabs or spaces, but you’ll want to put your preferred settings in your .vimrc.
Automatic text wrapping with
set wrap
. This is one of the most useful settings when writing LaTeX in Vim.Change out that ugly red column border with a nice grey column border with
set colorcolumn=+1
andhi ColorColumn ctermbg=7
.Show line numbers for easy reference:
set number
Set your preferred spell check default. As an American I do
set spelllang=en_us
Useful Vim commands
In addition to having a good setup for Vim, here are a few commands that I am constantly using.
Search:
/foo
Search and replace:
:%s/foo/bar/g
Toggle line numbers:
set nu
,set nu!
Enable pasting without auto-indentation:
set paste
,set paste!
Turn on and off spell checking:
set spell
,set spell!
Automatically wrap a block of text: First select with
v
for visual, and then usegq
to perform the wrapping.Indent an entire section: First select with
v
and then indent with>>
.
Automatic LaTeX compilation
This isn’t Vim specific, but you’re going to need a complation method to turn your LaTeX in to a PDF. On Ubuntu this works very well for me:
Install texlive and latexmk from your package manager. This will require more than a Gigabyte of space on your disk and so will take a while, but that sure beats having to selectively install LaTeX packages.
sudo apt-get install texlive-full latexmk
Start a background process to automatically compile your LaTeX and generate a PDF. You can edit your document in a separate terminal tab and have the PDF on the other side of your screen. Seemless editing!
latexmk -pvc -pdf main.tex &