Syntactically Aware Pagination

Another short and simple tutorial post (hopefully). I recently decided I wanted colorful cat-ing, and ended up doing myself one better - colorful (syntax-highlighted) paging through less / more. Here’s a taste:

On a side note… god, do I wish Flickr wouldn’t make screencasts look like absolute shit by re-encoding them to the horrible .FLV format. C’mmon Flickr, where’s the h.264 love?

Anyway, as for how to achieve this - it’s not difficult. You need the highlght library, which you can get from your favourite package-management system - for me, on a Apple Macintosh using MacPorts, I just have to run the following:

sudo port install highlight

Once you’ve installed highlight, just add a file with the following content to a file named page in your $PATH (you will, of course, need to adjust the paths to whatever suits your package installation method - which highlight may help you there):

#!/usr/bin/env bash
/opt/local/bin/highlight --ansi --force "$*" | /opt/local/bin/less -r

Finally, add something like the following to your .profile:

export PAGER=page

All of this information can be found compacted into the commit to my personal dotfiles repository with the changes mentioned here - if you are interested in what changes (if any) I’ve made to this concept since I posted this, check the files mentioned in this changeset. Specifically, this is my page script, any changes I decide to make to it will appear there.