less Command – Syntax Highlighting in less Command

bashlesssyntax highlightingvivim

I need to use the less command with the syntax highlighting of the vim command for python, C, bash and other languages.

How do I apply syntax highlighting colors according to vim colors for less command?

Best Answer

Syntax highlighting of less, works just fine on most *nix systems.

apt install source-highlight
export LESSOPEN="| /usr/share/source-highlight/src-hilite-lesspipe.sh %s"
export LESS=' -R '

On Fedora/RedHat based distros use /usr/bin/src-hilite-lesspipe.sh instead.

Even on Cygwin you can do it with the minor adjustment of the shell script path and installing with apt-cyg instead of apt.

However, using this drastically slows down browsing of large files. I suggest to use alias in such a way to only implement the LESSOPEN export above when needed, like this:

alias lessh='LESSOPEN="| /usr/bin/src-hilite-lesspipe.sh %s" less -M '

where the -M flag is convenient to also show filename and line number.

Also remember to copy the script into your bin path:

cp /usr/share/source-highlight/src-hilite-lesspipe.sh /usr/bin/src-hilite-lesspipe.sh

UPDATE: 2019-07-24

Apparently, on more recent Cygwin installs, you have the following files in your path:

source-highlight.exe
source-highlight-esc.sh
source-highlight-settings.exe

So now you also need to execute the source-highlight-settings.exe that will add the configuration file:
$HOME/.source-highlight/source-highlight.conf.

Related Question