Vim live preview (sort of)

October 8, 2014


When editing Markdown files or internet drafts in Pandoc’s Markdown, I wanted to see some live preview window. I looked around a bit, but the solutions presented on the Internet, seemed to be insufficient, either to clumsy or don’t work at all.

My usual routine is: edit -> write -> make -> reload “rendered” file.

Turns out you can automate most of this. Vim has a feature: --servername <id> which allows you to send commands to another vim instance using that <id>. So we need two pieces to make this work.

  1. start a vim instance with --servername markdown and;
  2. some way to send commands to it when building the file, turns out makeprg is an excellent candidate for this, as I was already using Make.

So to start with (2), add an autocmd:

autocmd Filetype pandoc set makeprg=makepandoc\ markdown

Which calls this little shell script:

make && \
vim --servername "${1}" --remote-send '<C-\><C-N>:e<CR>'

And then (1) in a terminal, next to the one you’re typing in:

vim -R --servername markdown draft.txt

Now every time you call :make (after writing it), the other vim window reloads it’s file. Some things that obviously don’t work: scrollbind, so you need to manual scroll to the interesting area of your file.

The fancy stuff right now would be to show a screencast, but … no.

Vim  Preview  Markdown