Linux – How to run a headless gvim to get syntax highlighted code converted to HTML

gvimlinux

I want to generate HTML files in batch to show some gvim colorschemes in action. I took my current solution from the script at http://code.google.com/p/vimcolorschemetest/, but this will open open a gvim window for each colorscheme I want to process.

So far, the only way I have found to avoid the annoying new windows that pop up every second is to start a VNCserver and set the DISPLAY environment variable to that of the VNCserver so that all gvim windows are sent to the display within the VNC session.

However, I would like to know if there is a way I can avoid the whole VNC setup and just run a headless gvim instance that does the conversion and exits, with no windows ever being actually displayed.

I'm using linux, BTW.

Best Answer

vim -E -c "TOhtml" -c "w" -c "qa!" -- test.c >/dev/null

Use vim, it will load faster than gvim. You can speed up the loading time a bit by using -X, or a No-X version of vim.

In order to silence it, use >/dev/null. But that will make vim complain (Vim: Warning: Output is not to a terminal) and pause a bit, hence we use -E.

We could have tried -E -s, but I'm somehow unable to use :TOhtml properly, the result is uncolored, and is one a single line.

I pimp the command a little by using -R -c "set noreadonly" or better -n alone. This prevents the usual warning message when opening a file that already has a swapfile. There is nothing particular to prevent failures to open a non writable file (eg. owned by root).

See my vimcat repository at GitHub, for an example usage of vim syntax highlighting used in a terminal.

Related Question