Syntax highlighted printing with vim

When I need to understand code I usually print it and printed code is easier to understand if the code syntax highlighted. Before I learned this vim trick I usually either used vim built-in :TOhtml function and opened the syntax highlighted code in a web browser but it turned out that vim has another tric up the sleeve. The :hardcopy or :ha command.

First I need to set the printer in my .vimrc file:

set pdev=cp01
set printoptions=paper:A4,syntax:y,wrap:y, duplex:long

Now I can just use the :ha command print the highlighted document directly to the printer cp01.

Print to PDF
In the recent versions of Ubuntu Linux the Cups installation have had PDF printer device which basically just wrote the output to PDF file an put it in /home/username folder. So by using the follwing code I can make vim make a nice PDF version of the text.

set pdev=pdf
set printoptions=paper:A4,syntax:y,wrap:y, duplex:long
This entry was posted in Articles. Bookmark the permalink.

3 Responses to Syntax highlighted printing with vim

  1. Ras says:

    A propos så fandt jeg et par handy kommandoer til .vimrc i dag:
    autocmd BufEnter *.tex syntax match MyTeXSection “\\.*section.*}”
    autocmd BufEnter *.tex map :!grep -l “\\documentclass” *tex \| xargs

    Den ændrer syntax-highlighting i .tex dokumenter så \section{..}, \subsection{..} etc bliver highlightet med en fin blå farve. Nyttigt ved redigering af store .tex filer.

  2. Ras says:

    Hov .. det var dem her jeg mente:
    autocmd BufEnter *.tex highlight MyTeXSection guifg=black guibg=lightblue
    autocmd BufEnter *.tex syntax match MyTeXSection “\\.*section.*}”

Leave a Reply