12

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

Travis Gockel
  • 26,877
  • 14
  • 89
  • 116

5 Answers5

10

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

ELLIOTTCABLE
  • 17,185
  • 12
  • 62
  • 78
7

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.

3

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

midenok
  • 930
  • 10
  • 14
  • 1
    Please don't just post some tool or library as an answer. At least demonstrate [how it solves the problem](http://meta.stackoverflow.com/a/251605) in the answer itself. – Zoe Nov 27 '18 at 18:18
  • 1
    @Zoethetransgirl `bat FILENAME` – c z Oct 23 '19 at 10:37
  • this works for me better than vimpager. for some reason bat wont syntax highlight typescript files though – Harry Moreno Jan 27 '23 at 17:59
2

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.

Tyler Eaves
  • 12,879
  • 1
  • 32
  • 39
  • 1
    Nice - jed supports piping into the buffer (vim and regular emacs do not). How does one turn on syntax highlighting in jed? If I `jed ${FILE}`, highlighting works, but not when I `cat ${FILE} | jed` – Travis Gockel Mar 15 '11 at 18:49
  • I don't think that's possible. It's actually a really hard problem, since it's fairly straightforward to come up with short programs that are syntactically valid in many languages. The mode detection, as in most editors, is just based on file extensions. Easiest way would probably be to just pipe stdout to a file with an appropriate extension. – Tyler Eaves Mar 15 '11 at 18:55
  • What is the command sequence to turn on syntax highlighting once the program has already started? I'm not familiar with emacs derivatives. – Travis Gockel Mar 15 '11 at 19:01
  • Not sure actually. Been a while since I've really used it. Doing an `info jed` brings up fairly extensive documentation. – Tyler Eaves Mar 15 '11 at 19:03
1

Jed has syntax highlighting modes for different languages, simillar to the emacs ones. For example if you piped a C program to it, you can turn on the highlighting by pressing 'ESC', then 'x', then typing 'c-mode' . If it is a php program - change the last part to 'php-mode' and so on ...

Dangelov
  • 701
  • 6
  • 14