Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Does anyone know of a simple way to produce the PostScript corresponding to a syntax-highlighted version of a source file that can be piped directly to a PostScript printer?

As the wording of the question above probably suggests, I'm looking for something that I can run from the command line. I'm thinking of an interaction like:

% syntax_highlight <SOURCE_FILE> | lp

...with command-line switches as needed, etc.

The best I've found so far is a Unix utility called highlight, but it has problems. The most serious of it is that it doesn't have an option to output PostScript directly. (Since highlight does support LaTeX output, I tried to patch together a script that would automate the process of generating the PostScript file via *.tex => *.dvi => *.ps, but the visual appearance of the final result is awful, much worse than it is for the HTML file that highlight generates for the same source code input.)


share|improve this question

migrated from programmers.stackexchange.com Sep 10 '11 at 15:29

This question came from our site for professional programmers interested in conceptual questions about software development.

up vote 12 down vote accepted

You can use vim.

vim -c hardcopy -c quit /path/to/file

This will print the file and quit immediately. By default, vim prints with syntax highlighting.

If you need to print from stdout of some command, you can do this:

cat some_file.c | vim -c hardcopy -c 'quit!' -

If you want to save the .ps for later, you can do that by adding redirection to the hardcopy command, like so:

vim -c 'hardcopy > /path/to/saved.ps' -c 'quit' /path/to/file

Vim lets you set lots of printing-related options, so you might want to see the documentation if you want to tweak it. Of course, there are lots of syntax highlighting options as well.

share|improve this answer

There are several programs that pretty-print various programming languages to Postscript, which don't require any third-party software to run:

If you are willing to go via LaTeX, you have more options. Going via LaTeX is mostly useful if you want to include code and something else in the same document; otherwise it's overkill.

share|improve this answer

In Gedit, the standard editor for gnome, you can print to file, choose Postscript (default PDF) and mark on the third tab, to use highlightening, which is off as default, which produces a nice ps-file for me.

And Gedit has a lot of syntax files, to serve many programming languages, HTML, SQL, XML, rc-files, you name it.

share|improve this answer
This works, except that for me (gedit 3.10.4 on Ubuntu 14.04.4) the generated PDF is in grayscale, not color. Also printing directly results in grayscale. – Marnix Klooster May 12 at 17:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.