I was just quickly flipping through code on a terminal and got to thinking that it would be pretty handy to be able to syntax highlight when using head or grep without having to open an editor. Luckily, I remembered Pygemtize was pretty handy when syntax highlighting in LaTeX so I wondered if they had an equivalent output for terminal. It turns out they do (in standard or 256 color no less) so in a couple minutes I had a really short script for highlighting code at the terminal saved it to bin/ccat and was ready to go. Pygmentize really is impressive. Here's the script if anyone else is looking to do the same:
- #!/bin/bash#!/bin/bash
- if [ ! -t 0 ];then
- file=/dev/stdin
- elif [ -f $1 ];then
- file=$1
- else
- echo "Usage: $0 code.c"
- echo "or e.g. head code.c|$0"
- exit 1
- fi
- pygmentize -f terminal -g $file
It just looks for stdin and if it doesn't find it it looks for a file name or takes direct input. Obviously it requires Pygmentize (which is really easy to install if you already have Python).
Erik | 23-Oct-09 at 11:28 am | Permalink
Great, thanks for the tip! I hadn’t heard of Pygments before – now I’ve got some work to do in customizing these colors :-)
bob | 29-Dec-09 at 6:20 am | Permalink
Nice, didn’t know Pygmentize worked with terminal.
rych | 20-Apr-10 at 3:50 pm | Permalink
Nice, thanks for publishing — a nice shortcut to not have to open an editor.