Convert markdown files to pdf files using pandoc in vim

2024-08-28
2 min read

I am learning vim and I want to migrate my workflow in Sublime Text to vim. One of the things I do most with Sublime Text is to write markdown files and convert them to pdf files using Pandoc. After some search, I did not find a satisfied plugin which does such a job. The vim-pandoc seems to be too powerful. I did some more search and found this post, which is very informative and I follow the instructions to create my own plugin to convert markdown files to pdf files using pandoc in vim.

The approach is quite straighforward. Pandoc as a commandline tool can be called within vim.

:!pandoc '%' -o '%:r'.pdf

The % represents the current file together with its path and %:r stands for the current file with the extension (i.e., .md in this case) stripped. However, type such a long command each time would be very time-consuming and annoying. To more conveniently call it, the command can be put in a vim compiler plugin.

The vim compiler plugin is put inside .vim/after/compiler and the compiler contains the following commands.

let current_compiler="pandoc-md"
CompilerSet makeprg=pandoc\ -F\ pandoc-crossref\ --pdf-engine=xelatex\ --metadata-file='~/Documents/notes/template/metadata.yaml'\ -H\ '~/Documents/notes/template/head.tex'\ '%'\ -o\ ~/Documents/notes/pdf/'%:t:r'.pdf
CompilerSet errorformat=

First, I define a compiler named pandoc-md and then the command to run when calling :make in vim. The makeprg is very long and needs some explanation. All spaces in makeprg need to be escaped with a slash \, just as in .vimrc when we define what to display on the statusline. Since my pdf files use customized layout, I also specified some metadata and a \LaTeX template with the --metadata-file and -H flags. The paths of these files should be inside quotation marks.

I don’t want to get pdf files with the names filename.md.pdf, so I use %:r.pdf to refer to the file name without the extension. Also, as I want to put all my pdf files in a pdf folder, I specified the folder in makeprg, but note that this path, unlike those of the metadata file and the \LaTeX template, is not inside quotation marks.

Another thing is that I use :t to refer to the file name without the full path [^cmd-special]. Therefore, the resulting file would be put in the folder ~/Documents/notes/pdf.

One last thing is to create an autocmd to set the compiler for markdown files only. I put the following line in my .vimrc, so that each time I run :make each time within vim, the markdown file will be automatically converted to pdf files and stored in the specified folder.

autocmd FileType markdown compiler pandoc-md

A sidenote: Convert markdown files to pdf files in Sublime Text

Sublime Text has a very convenient build system which I use to convert markdown files to pdf files. Click Tools on the toolbar and then to Build System and New Build System to create a new system. Put the following commands in the file and save it as Pandoc.sublime-build.

{
    "working_dir": "~/Documents/notes/pdf",
    "shell_cmd": "pandoc -F pandoc-crossref --pdf-engine=xelatex --metadata-file='~/Documents/notes/template/metadata.yaml' -H \"~/Documents/notes/template/head.tex\" \"$file\" -o \"$file_base_name.pdf\" && open -a Preview \"$file_base_name.pdf\"",
    "selector": "text.html.markdown",
    "path": "/usr/texbin:$PATH"
}

References and related posts