Syntax highlighting in terminal
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 Pygmentize 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:
[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
[/bash]
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).