My vim setup for writing Latex

Souvik Haldar
3 min readOct 21, 2020

Introduction

Latex is a powerful way of writing technical documentation, or probably any document at all. The best feature of Latex is that all you need to worry about writing a document is the content, the design, the citation, the reference, etc is all taken care of by the latex.

There are many ways to go about writing a latex file, but I’m very very comfortable with writing anything on the computer with vim. I absolutely love vim for any kind of editing task. That’s why whenever possible I try to use vim for writing or use vim key emulation when using any other software except vim. I use vim emulation in the terminal, in the note-taking app (Obsidian) and now I’m trying to figure out a way to use vim emulation in slack :P

So, naturally, my goal was to set up latex writing, auto compilation, and automatic PDF generation, all in vim. If you find yourself in my shoes then follow along, the end result should look similar to the image below ❤

Whenever you makes changes to latex file on left and save it (:w) the changes reflect in real time in the pdf on right :)
Whenever you make changes to the latex file on the left and save it (:w) the changes reflect in real-time in the pdf on right :)

Prerequisites

The following are the pre-requisites for the required setup:

  1. vim
  2. Latex
  3. latexmk
  4. asyncrun vim plugin
  5. Evince PDF reader (you can use Preview, mupdf, etc)

Steps

  1. Install and setup all the items listed above on your respective OS. I’ve tested it on Mac and Ubuntu and it works great.
  2. I want to use Evince PDF reader for previewing the changes, so I need to add the following line to ~/.latexmkrc file:
    $pdf_previewer = ‘start evince’;
    You can customize latexmk according to your need, follow here.
    For Mac, I would suggest using Skim which can be installed using brew install skim and follow the above link for setting up the synchronization.
  3. Open a latex file, for eg. first.tex
  4. Now you need to compile the Latex file then generate the PDF output, here is the magic to automate the process of compiling and generating the changes in the PDF file in realtime.
    i. You can open another terminal and run the following command to compile and generate the PDF:
    latexmk -pvc -pdf <path-to-latex-file>
    But if you want to do it in vim (that’s the swag right ;)) you can run the above shell command asynchronously using the asyncrun vim plugin as mentioned in the pre-requisites. Using this plugin you can run any process asynchronously, hence it is a useful plugin to have anyway. Here we will use it to run the latexmk shell command.
    You can do that using:
    :AsyncRun latexmk -pvc -pdf %
    Here % takes the path to the currently open file automatically. You can provide the path explicitly too.
  5. Now you can see the PDF reader having opened the compiled file:) Now you can make changes, then save it using :w and the changes will reflect automatically!
    Since you will be using the latexmk command very often, it is wise to map it to some key. Here I’ve mapped it to Leader + c + p using the following map command in the vimrc file:
    nnoremap <Leader>cp :AsyncRun latexmk -pvc -pdf %<CR>
    Now you can just hit Leader + c + p keys to do point 5.
  6. If you want to see the logs of the compilation, which is very important to look for any errors in latex file, you can open the quickfix window, since asyncrun plugin shows the running process here only.
    Do :copen to open the quickfix window.
  7. You can close the latexmk process using :Asyncstop command. Explore the asyncrun plugin here

Conclusion

I’m fairly new to the world of Latex and vim, hence there is a lot to learn yet. I have not used any latex plugin since my requirements were quite simple, probably I’ll explore them when needed, currently, I’m quite content. Also, it would be wise to add macros to writing latex, I’ll do them when the time comes, now I manually write everything.
Let me know on Twitter if I were of any help or just to say Hi :)

--

--