Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax Highlighting Pager [closed]

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...

like image 588
Travis Gockel Avatar asked Mar 15 '11 18:03

Travis Gockel


4 Answers

Might I suggest vimpager?

First off, recent vim distributions (I believe 6.0 and above) come with a pager-esque-mode script. It's quite simple and functional, and operates similarly to less. Try: vim '+help less' +only.

Even better, however, Rafael Kitover has written a much more robust and powerful script called vimpager. It's available on GitHub (or vimscripts). If you are on OS X and using Homebrew, it's as easy as brew install vimpager.

At that point, you can simply set $PAGER=vimpager, or even alias less=vimpager. It works excellently.

enter image description here

like image 112
ELLIOTTCABLE Avatar answered Nov 15 '22 01:11

ELLIOTTCABLE


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.

like image 8
StackExchange saddens dancek Avatar answered Nov 14 '22 23:11

StackExchange saddens dancek


You might try using jed. Yes, it's a text editor, not a pager, but it's quite lightweight and the default install contains excellent colorschemes for a wide variety of file types and languages.

like image 2
Tyler Eaves Avatar answered Nov 15 '22 01:11

Tyler Eaves


Look for bat: A cat(1) clone with wings.

bat supports syntax highlighting for a large number of programming and markup languages.

It is not a pager, but it automatically redirects output to less if needed.

enter image description here

like image 2
midenok Avatar answered Nov 14 '22 23:11

midenok