DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,274 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Viewing more with less - color syntax
Vedran Vidovic
Vedran Vidovic

Posted on

Viewing more with less - color syntax

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:

JSON

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:

YAML

Top comments (0)

πŸ‘‹ Hey, my name is Noah and I’m the one who set up this ad. My job is to get you to join DEV, so if you fancy doing me a favor, I’d love for you to create an account.

If you found DEV from searching around, here are a couple of our most popular articles on DEV: