You can use the package, e2ansi, that provides
syntax highlighting support for pagers like
The package use the mother of all text editors, Emacs, to
perform the actual syntax highlighting. As an added bonus, all other
conversions normally performed by Emacs -- like uncompressing files --
is also performed.
The following is the result of viewing a file using
The package provides a command-line tool
e2ansi-cat that starts
Emacs in batch mode, opens files, syntax highlight them, and renders
the result using ANSI sequences.
You can integrate this into
less by setting the following variables
to, for example (the location of your init file may vary):
export "LESSOPEN=||-/usr/local/emacs --batch -Q -l ~/.emacs -l bin/e2ansi-cat %s"
alias "more=less -X -E"
In the configuration above,
less restores the original terminal
window content whereas
more simply output new content after the
Note: If you use an old version of
less, it might not support the
|| or the
- syntax, in which case you may need to use simply
less in pipes
The "-" character in
LESSOPEN indicates that the input filter should
also be used when piping text into
less. In this case, Emacs can
only rely on text itself (and not a file name). Fortunately, Emacs
provides a system for this. In addition, the provided file file
e2ansi-magic.el sets up additional file types. For example:
Why use Emacs?
- Emacs has support for virtually all programming languages and
structured text formats. In most cases, the syntax highlighting
support is excellent.
- You can easily add support for more languages and formats, or modify
existing packages to suit you needs.
- Emacs support color themes. When using
e2ansi, the colors in the
theme is preserved when viewing a file in
less. You can pick a
suitable color theme from a number of sources, or design your own.
- If you use Emacs as your editor of choice, you will get the same
highlighting in the editor as you get when viewing a file using
less (minus limitations in ANSI sequence format and the terminal
less will take advantage of Emacs features that perform automatic
conversion, for example uncompressing files. In fact, you can teach
Emacs to perform any kind of conversion like automatically convert a
binary file to human readable form using an external tool.
- You can view files located on other machines by using Emacs' syntax
for remote access like
e2ansi is distributed on Melpa and can be installed using
the standard Emacs package system
e2ansi is hosted on GitHub
e2ansi page on the Emacs Wiki
- The home of
Operating system notes
On MS Windows, the console does not natively support ANSI sequences.
less application is capable of rendering them. I
am not aware of any contemporary binary distribution of
MS Windows and the provided build files is hard to use. Fortunately,
it's easy to build
less using CMake, see this text for
OS X distributes an ancient version of
less. Fortunately, it's
easy to build a modern version directly from the source.