TextMate and vim are my favorite text editors. Unfortunately, TextMate has very little support for printing. It's a nightmare, really, and unlikely to be fixed any time soon. The author simply isn't particularly interested in printing.
Fortunately, vim is really good at printing, at least to PostScript, and does syntax highlighting better than just about anything else. And fortunately, TextMate is designed to let its users easily hack together their own supplementary solutions, called Bundles.
My solution to the printing problem is, in short:
- Print from vim to PostScript.
- Convert the PostScript to PDF.
- Open the PDF in Preview.
- Do all this with the standard command-P shortcut.
First, here's the bundle item:
[ -n "$TM_FILEPATH" ] && \ PDF_FILE=~/Desktop/`basename "$TM_FILEPATH"`.pdf PS_FILE=/tmp/`basename "$TM_FILEPATH"`.ps vim \ "+set number" "+syntax on" "+color slate" \ "+set printoptions=number:y" \ "+set printfont=courier:h9" \ "+hardcopy > $PS_FILE" "+q" \ $TM_FILEPATH &>/dev/null && \ ps2pdf $PS_FILE $PDF_FILE && \ rm $PS_FILE && \ open $PDF_FILE
And here's a screenshot to make it even easier:
To get this working on your own Mac, you need to install the xpdf package from MacPorts, and then find a suitable color scheme for vim printing if you don't like the default. There's a great write-up on that last bit over on the Vim Tips Wiki. Once you've installed/tweaked/tested your scheme, just replace the "slate" reference in the bundle text above.
LIMITATIONS, QUIRKS AND BUGS
- This prints in 9pt Courier, with line numbers and wrapping, because that's how I think code should be printed.
- Only saved files are printed; your "buffer" is not. I may change that later.
- It will probably break if the slate color scheme is not installed. That was the best scheme installed by default on my Mac, hence the choice.
- The PDF is saved to your Desktop, overwriting any like-named PDF, e.g. MyModule.pm.pdf. I like having the thing on my desktop, but I may make it clobberproof later.
- YMMV, it's probably buggy, and so on. If you aren't comfortable hacking UNIX, don't try this at home. I actually don't think it's good enough yet to submit to the bundle repository, so don't make any assumptions.