Cover image for Viewing more with less - color syntax

Viewing more with less - color syntax

vvidovic profile image Vedran Vidovic ・2 min read

If you are using Linux command-line a lot (out of love or out of necessity) you probably use less command a lot too. When you need to check the contents of some file less is here to help you to do it quickly. Maybe you didn't know but less can do more than help you view your text files, for example, you can check the contents of your archive using less:

$ less go1.13.7.linux-amd64.tar.gz

View archive contents

Syntax highlighting for less - source-highlight

The only issue I had with the default less configuration on my Linux machines (Ubuntu 16.04 & 18.04) was that all of my source files were shown without any syntax highlighting. For me it was not just an issue of form, it was an issue of function - syntax highlighting makes reading source files much easier.

Fortunately, for Linux environments, you can use a GNU Source-highlight project which can be used by less command to add syntax highlighting to files opened using less command.

Just install syntax-highlight for your favorite Linux distribution and configure two environment variables less uses to get syntax highlighting in your favorite pager command.

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

If you want to have these settings applied permanently to your system add something like this to your ~/.bashrc or ~/.profile file:

# less syntax highlighting + source-highlight installation
export LESSOPEN="| /usr/share/source-highlight/src-hilite-lesspipe.sh %s"
export LESS=' -R'

The result can be easily seen if you open for example JSON file and compare how less command shows it with highlighting vs. without highlighting:


Customizing source-highlight - adding new languages

There are some languages that you won't find on the list of supported languages for the source-highlight project. Often, you can find a solution to this problem with some help from the development community on the Internet. For example, YAML is not supported by default but you can find a solution created by user tkfm-yamaguchi and available as a GitHub gist. Basically you have to add make two small changes (one new configuration (language) file and small change of the existing configuration file) to enable syntax highlighting for your YAML files.

The result is visible below:


Posted on by:

vvidovic profile

Vedran Vidovic


Creating software for most of this millennia and still love to code. Working on projects of different sizes and complexity though mostly in the enterprise integration area.


markdown guide