Home » Questions » Computers [ Ask a new question ]

How to syntax highlight via Less

How to syntax highlight via Less

is there a way to syntax highlight a file I see in Less?

Asked by: Guest | Views: 240
Total answers/comments: 5
Guest [Entry]

"You can use GNU's source-highlight, as shown here (path may differ, see below):

export LESSOPEN=""| /usr/bin/src-hilite-lesspipe.sh %s""
export LESS=' -R '

As of Debian Stretch and Fedora 25, package names and script paths differ

Debian:

sudo apt install libsource-highlight-common source-highlight
dpkg -L libsource-highlight-common | grep lesspipe
# /usr/share/source-highlight/src-hilite-lesspipe.sh

Fedora:

sudo dnf install source-highlight
rpm -ql source-highlight | grep lesspipe
# /usr/bin/source-highlight/src-hilite-lesspipe.sh"
Guest [Entry]

"pygmentize somefile.ex | less -R

or

function cless () {
pygmentize -f terminal ""$1"" | less -R
}

Pygmentize comes as a part of the Pygments Python syntax highliter."
Guest [Entry]

"I was also searching for this and found another solution using Vim:
http://ubuntu-tutorials.com/2008/07/14/use-vim-as-a-syntax-highlighting-pager/

The post is rather old, so now on more recent distros vim 7.2 is shipped and the .bashrc will read:
alias vless='vim -u /usr/share/vim/vim72/macros/less.vim'"
Guest [Entry]

"I'm glad to announce a new package, e2ansi, that provides
syntax highlighting support for pagers like more and less.

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.

Example

The following is the result of viewing a file using less and
e2ansi:

Configuration

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""
export ""LESS=-r""
alias ""more=less -X -E""

In the configuration above, less restores the original terminal
window content whereas more simply output new content after the
prompt.

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
LESSOPEN=|/usr/local/emacs ....

Using 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
window).
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 /USER@HOST:FILENAME.

Useful links

e2ansi is distributed on Melpa and can be installed using
the standard Emacs package system
e2ansi is hosted on GitHub
The e2ansi page on the Emacs Wiki
The home of less.

Operating system notes

On MS Windows, the console does not natively support ANSI sequences.
Fortunately, the less application is capable of rendering them. I
am not aware of any contemporary binary distribution of less for
MS Windows and the provided build files is hard to use. Fortunately,
it's easy to build less using CMake, see this text for
details.
OS X distributes an ancient version of less. Fortunately, it's
easy to build a modern version directly from the source."
Guest [Entry]

"If you have GNU Source-highlight installed you can use the following command to highlight the syntax of a single file:

$ src-hilite-lesspipe.sh yourfile.xml | less -R"