Search code examples
command-linesyntax-highlightingpager

Syntax Highlighting Pager


Right now, I use most as my pager. While it has helpful syntax highlighting for man pages, it lacks colored syntax highlighting for anything else (I'm specifically looking for diff/C++).

Meanwhile, pygments is a wonderful program. I can easily make colorized output with it:

# ./pygmentize -f console256 ${file}
hg diff | ./pygmentize -f console256 -l diff

Now, I would like to be able to page the output, so I just use:

# ./pygmentize -f console256 ${file} | most
hg diff | ./pygmentize -f console256 -l diff | most

At this point, most dumps all the colorizing control characters to my screen like so:

^[[38;5;28;01mclass^[[39;00m ^[[38;5;21;01mheap_allocator^[[39;00m
{
^[[38;5;28;01mpublic^[[39;00m^[[38;5;241m:^[[39m

This is, of course, unreadable. I looked though the man page for most, but I couldn't find any "Hey, show those control characters as colors instead of printing them" options. less has the same garbage behavior as most, but more shows the colors perfectly fine, with the obvious limitations of being more.

Is there a pager that supports syntax highlighting or some crazy combination of parameters and programs I can string together to make this work? Ultimately, I would like to get diffs and logs from Mercurial to be highlighted, so maybe there is a shortcut in there...


Solution

  • less -R shows ANSI color sequences as-is (instead of expanding to caret notation). That'll make syntax highlighting work!

    You can also create an environment variable LESS=-R to make this the default behavior. Similarly for other options; see man less.