Wednesday, June 25, 2008

Printing with TextMate, vim and Friends.

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:

  1. Print from vim to PostScript.
  2. Convert the PostScript to PDF.
  3. Open the PDF in Preview.
  4. 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:

TextMate Bundle

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

  1. This prints in 9pt Courier, with line numbers and wrapping, because that's how I think code should be printed.
  2. Only saved files are printed; your "buffer" is not. I may change that later.
  3. 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.
  4. 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.
  5. 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.

1 comment:

Unknown said...

this really helped me out
thanks a lot!