Syntax Highlighting Pager [closed]
Asked Answered
C

5

13

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

Collenecollet answered 15/3, 2011 at 18:35 Comment(0)
E
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.

Escolar answered 22/3, 2011 at 22:55 Comment(1)
I don't see this behavior on macJook
L
9

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

Landri answered 29/5, 2013 at 1:19 Comment(0)
E
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.

Escolar answered 22/3, 2011 at 22:55 Comment(1)
I don't see this behavior on macJook
P
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

Pfennig answered 27/11, 2018 at 18:16 Comment(3)
Please don't just post some tool or library as an answer. At least demonstrate how it solves the problem in the answer itself.Glennaglennie
@Zoethetransgirl bat FILENAMEZoba
this works for me better than vimpager. for some reason bat wont syntax highlight typescript files thoughJook
A
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.

Anitaanitra answered 15/3, 2011 at 18:41 Comment(4)
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} | jedCollenecollet
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.Anitaanitra
What is the command sequence to turn on syntax highlighting once the program has already started? I'm not familiar with emacs derivatives.Collenecollet
Not sure actually. Been a while since I've really used it. Doing an info jed brings up fairly extensive documentation.Anitaanitra
L
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 ...

Langbehn answered 31/3, 2012 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.