You want to print something from Vim.


In GVim there's a Print entry on the File menu, and a printer icon on the toolbar. In Vim execute :hardcopy.


:hardcopy converts the current file to Postscript and sends it to the default printer. You can specify that a different printer is used with :set pdev=printer. For example, :set pdev=usblp1.

If you have a PDF printer installedLinux/Mac users can install the CUPS PDF package to get a PDF printer, e.g. on Debian/Ubuntu: apt-get install cups-pdf., you can use this technique to print a file to PDF. For example, assuming your PDF printer is called pdf:

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

The printoptions line is a comma separated list of values that affect how Vim formats the document before it sends it to the printer. A list of options is at :help popt.

The paper option sets the paper size. It accepts values such as A3, letter, and legal.

The syntax option determines whether the document is printed with syntax highlighting. By default it has the value a which means that Vim only uses highlighting for colour printers. A value of y forces highlighting.

Lines are wrapped when wrap:y, which is the default. If wrap has the value n, long lines are truncated.

Other useful options are header:0 to stop a header from being printed, number:y to number lines, duplex:off to print on only one side of the page.

On Linux the lpr utility is used for printing. To use a different program add a stanza such as the following to your vimrc:

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

The above example specifies that the GNU Anything to Postscript (a2ps) utility is used for printing. a2ps can print two pages per physical page and custom headers, for example; see man a2ps for a complete list of features.