October 2009

Syntax highlighting in terminal

Syntax highlighted cat 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).

Bash/UNIX
Programmer

Comments (7)

Permalink

Happy (late) October 1st

China just had their national day (called GuoQinJie I’m told) on October 1st. It was their 60th anniversary so there was a pretty big parade. Which is actually a bit frustrating since I was in Tienanmen Square last October 1st and there was nothing but a bunch of tourists milling around (the rest of the trip was great anyway so no big deal). Apparently if you’re ever planning on visiting Beijing on National Day, you need to make sure it’s in a year ending in 9 or 4. Anyway, my wife came across this pretty cool time lapse video of the parade (the video page has it in HD):

Tourist

Comments (0)

Permalink