GVim and printing

Posted by fitheach on Mon 06 August 2012

The last time I printed directly from GVim, or for that matter Vim, was so many years ago that I had forgotten the details of how it works. Most of my content in GVim is either never printed or it gets compiled to Postscipt or PDF and only then is printed. It therefore surprised me today when I pressed the Print button and I didn't get a print options GUI.

Traditionally, Vim uses the command :hardcopy to print to the default printer. The default printer can be defined in ~/.vimrc as follows:

set pdev=hplj2430set printoptions=paper:A4,syntax:y,wrap:y


  • pdev is the exact printer name
  • syntax:y means colouring etc. is employed
  • wrap:y means text longer than one line is wrapped. The alternative is the text is truncated.
  • Further options can be found by using help :help print

If you want to print to a different printer you can use the "set pdev" command again, for example :set pdev=hplj4300 remembering to get the name correct. Hmmm, not so friendly.

Instead, I added the following to my ~/.vimrc file:

set printexpr=PrintFile(v:fname_in)function PrintFile(fname)  call system("gtklp " . a:fname)  call delete(a:fname)  return v:shell_errorendfunc

Where gtklp is a front-end to the CUPS system which then gives me access to any of the printers on the network, along with their options, from a nice GUI. If you don't use GTK you could try kdeprint (for KDE3), printer-applet (KDE4/Qt) or xpp (X11 generic) although I haven't tested these other utilities. Now no matter whether I use the Print button, File > Print from the menu or use the command :hardcopy I get the gtklp GUI pop-up.

/ fitheach