11

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.)

Thanks!

kjo
  • 15,339
  • 25
  • 73
  • 114

4 Answers4

14

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.

Shawn J. Goff
  • 46,081
  • What about adding -n -R flags to such vim invocations? Also I have problem with running this when I have already opened file with another vim (I raised question here: https://unix.stackexchange.com/q/676810/9689 ) – Grzegorz Wierzowiecki Nov 09 '21 at 19:45
  • While vim, as far as I can tell, still has that feature, it should be noted that it has been removed in neovim since version 0.9.0. – CodenameLambda Apr 08 '23 at 03:52
6

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.

3

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.

user unknown
  • 10,482
1

Expanding on Shawn J. Goff's answer:

You can use the following single-line vim command to create a .ps file from within vim:

:hardcopy > %.ps

If you prefer a .pdf file, then you can do:

:hardcopy > %.ps | !ps2pdf %.ps && rm %.ps

Note:

  • The % is shorthand for the current filename, so HelloWorld.C will print to HelloWorld.C.ps or HelloWorld.C.pdf

Additionally, to change the rendered font, set the printerfont before executing the hardcopy command. For example, to select Courier 8:

:set printerfont=Courier:h8

Putting it all together, I opted to put the following in my .vimrc file so that I can simply execute the :HardcopyPs or :HardcopyPdf command (which can also operate on a selected range within a file):

set printfont=Courier:h8 "select the font to use when printing
command! -range=% HardcopyPs <line1>,<line2> hardcopy > %.ps && echo 'Created: %.ps'
command! -range=% HardcopyPdf <line1>,<line2> hardcopy > %.ps | !ps2pdf %.ps && rm %.ps && echo 'Created: %.pdf'
arr_sea
  • 121