less is the default pager for many Linux distributions with features including searching, text formatting, and color support.
In this tutorial, we’ll learn how to customize and add syntax highlighting to less. We’ll begin by going over ways to use less in combination with external commands. After that, we’ll discuss two methods of adding syntax highlighting to less.
2. Piping External Commands to less
Since less doesn’t provide built-in syntax highlighting, we may need to use an external command with less to achieve this. In this section, we’ll learn how to add color to less using a secondary program. Then, we’ll see how to persist these changes.
2.1. Adding Color With External Program
We can use an external command with less by piping its result to the pager.
Let’s try this out using grep:
$ grep -P 'alias|$' --color=always ~/.bashrc | less -R
In this example, we use grep with -P ‘alias|$’ to output the file with alias highlighted. Next, we ensure grep keeps its color when piped using –color=always.
Finally, we pipe the result to less, using -R to ensure that ANSI color escape sequences are displayed correctly.
Notably, the result includes color:
In summary, less now shows the ~/.bashrc file with alias highlighted. So, although less itself doesn’t highlight, it can support and interpret the respective highlight commands.
2.2. Automatically Running External Command With less
To run our grep command automatically with less, we add LESSOPEN and LESS to our environment:
$ cat ~/.bashrc
export LESSOPEN="| grep -P 'alias|$' --color=always %s"
To automatically set an environment variable, we can add it with export in our ~/.bashrc. Let’s break down the input preprocessor variable, LESSOPEN:
- |: pipes the output of the command to less directly without using an intermediary file
- grep -P ‘alias|$’ –color=always: command being piped to less
- %s: the input file
After that, we set LESS=’-R’, configuring less to always use the -R option in this context.
Now that we’ve set our environment variables, any time we call less with a file, it automatically highlights the word alias.
3. Syntax Highlighting Using pygments
pygments is a Python package that automatically detects and highlights syntax for over 570 programming languages and text formats.
In this section, we’ll install pygments and then learn how to use it to add syntax highlighting to less.
Before we can use pygments, let’s ensure it’s installed.
So, let’s begin by installing pygments with apt:
$ sudo apt install python3-pygments
In this case, to install a package, we use the apt install command. Since pygments is a Python3 package, its package name is python3-pygments.
Alternatively, pygments can be installed using pip.
3.2. Syntax Highlighting
Once installed, we can automatically detect and highlight according to a given syntax with the pygmentize command:
$ pygmentize -g ~/.bashrc
In this example, we use pygmentize to output our ~/.bashrc file in the terminal with syntax highlighting. Additionally, we use the -g flag with pygmentize to automatically detect the syntax to use. This can be inferred from a number of hints, including the file extension.
To use this command with less, we set LESSOPEN as we did earlier:
$ cat ~/.bashrc
export LESSOPEN="|pygmentize -g %s"
After we set LESSOPEN, less highlights the syntax of files using pygmentize.
4. Syntax Highlighting Using bat
In this section, we’ll install bat and then learn how to use it to add syntax highlighting to less.
We begin by installing bat using apt:
$ sudo apt install bat
We can alternatively install bat from its sources.
4.2. Syntax Highlighting
After installing it, we can use bat to highlight the syntax of a file with less:
$ bat --paging=always --pager=less ~/.bashrc
We use bat with –paging=always to view the output of bat with a pager, then specify the pager as less with –pager.
As expected, we get the result in a formatted output with the respective highlighting along with line numbers:
bat adds additional formatting, including line numbers and headers. However, these can be disabled using the –style=plain option.
Moreover, we can also do the reverse – configure less to use bat automatically:
$ cat ~/.bashrc
LESSOPEN='|bat --paging=never --color=always %s'
In this example, we set –paging=never, preventing bat from opening in a pager. Then, we use –color=always, ensuring bat pipes to less with color.
Now, less automatically highlights the syntax of files with bat.
In this article, we learned how to add syntax highlighting to less using two different commands.
First, we went over ways to colorize less with an external command and then automatically with LESSOPEN. Then, we saw how to use pygmentize and then bat to add syntax highlighting to less.