19 Apr 2013

Colorify less in Terminal.app

I don't know about you but I'm using less command in terminal window quite often. Especially it is very useful when you need to check quickly some parts of a source code. But the problem is that less command shows source code in plain text.
Recently I found this post about usage of pygments and less command. The given recipe tested on Linux and didn't work on my Mac with ZSH. After digging a little bit I drastically simplified it. Here it is:

  • We need to install pygments (http://pygments.org/). In my case it was simple: 
  pip install pygments
  • Add the following lines to .zshrc (in your case it could be .bashrc if you use Bash): 
export LESS="-R"
export LESSOPEN="|pygmentize -g %s"
The main difference from initial way is to rely on "-g" parameter of pygmentize instead of explicitly specification of lexer. Now my less command looks as follows:

Enjoy! Peace on you!