How to display man pages in color in Linux

Fatmawati Achmad Zaenuri / Shutterstock

If you want to highlight the color in your man Pages similar to syntax highlighting in an editor, there are two simple ways to accomplish this. We will show you both!

Color highlighting

Color highlighting makes things easier to read. You can make details stand out, so you don’t miss them and miss them. Most modern editors support syntax highlighting, which uses color to identify and differentiate the different elements of a programming language. Reserved words, variables, strings, and numbers are colored to facilitate visual analysis of a code page or function.

Have this feature on Linux man pages would be of great help. Despite favoring brevity, some man the pages are large, dense and difficult to read. Anything that makes it easy to visually navigate them is a good thing.

We are going to describe two ways you can get a colored effect on man pages. One involves using a different pager to display them, while the other requires passing a bunch of parameters to less at runtime. The best way to do this is to create a shell function.

The most pager is a file viewer, like more Y less, with improved handling of very wide files. It also colors automatically man pages.

Install most on Ubuntu, use this command:

sudo apt-get install most

Install most in Fedora, type:

sudo dnf install most

Install most in Manjaro, write:

sudo pacman -Syu most

To tell Linux to use most as the default locator, we have to export the value of the PAGER Environmental variable.

We write the following:

export PAGER=“most”

However, this only works until you close the terminal window. For this change to be permanent, we have to add it to the file “.bashrc” (we will make it the last line of the file):

gedit .bashrc

We add the line, save our changes, and then close the editor.

.bashrc in the gedit editor.

To activate the content of the modified “.bashrc” file, we close and reopen the terminal window.

To keep the terminal window open, we will use the source command, which can be shortened to a period (.). This will cause the shell to read the contents of the modified “.bashrc” file.

We write the following:

. .bashrc

Man color pages

Let’s open a man page and see how it looks:

man grep

The man The page opens as usual, but now has text highlighted in different colors.

manual page with color highlighting.

Scroll down and you will see how the different elements on the page are colored.

Using most it is very similar to using lessBut there are some differences. Press H on most to see a list of key combinations and their functions.

Use Color with less

If you don’t want to install another locator or have to learn new keystrokes, there is a trick you can use to force less use color. There are different ways to do this, but we will cover the fastest and easiest method.

This method uses the American Institute of National Standards (ANSI) color codes to control the on-screen effects associated with the old and mostly deceased termcap settings.

These were once used to specify how computer terminals of different makes and models should interpret display commands. The software packages also had their own termcap configuration, and less it does too.

Here are the definitions of less termcap settings:

  • LESS_TERMCAP_md: Starts the bold effect (double glow).
  • LESS_TERMCAP_me: Stops the bold effect.
  • LESS_TERMCAP_us: Starts the underline effect.
  • LESS_TERMCAP_ue: Stop the underline effect.
  • LESS_TERMCAP_so: Start highlighting effect (similar to reverse text).
  • LESS_TERMCAP_se: Stop the highlighting effect (similar to reverse text).

Again, we will configure them to control the color combinations using the American National Standards Institute (ANSI) color codes.

The color code format is easy to read once you understand it:

  • The leading ” e” identifies the sequence as a control code or escape sequence.
  • The “m” at the end of the sequence command indicates the end of the command. It also causes the control code to fire.
  • The numbers between “[” and “m” dictate which colors will be used. The colors are identified by number. Some numbers represent background colors and some represent foreground (text) colors.

These are the codes we’ll use to start a color sequence, and how to turn them all off:

  • ‘e[01;31m: Black background, red text.
  • ‘e[01;32m: Black background, green text.
  • ‘e[45;93m: Magenta background, bright yellow text.
  • ’‘e[0m’: Turn off all effects.

We’re going to wrap all of this in a shell function we’ll call man. It will set these values for us, and then call the real man program.

If you’ve already got some shell functions defined in another file, you can add this one to that file. Otherwise, copy the following text into the bottom of your “.bashrc” file:

man() {
    LESS_TERMCAP_md=$'e[01;31m' 
    LESS_TERMCAP_me=$'e[0m' 
    LESS_TERMCAP_us=$'e[01;32m' 
    LESS_TERMCAP_ue=$'e[0m' 
    LESS_TERMCAP_so=$'e[45;93m' 
    LESS_TERMCAP_se=$'e[0m' 

    command man "$@"
}
gedit .bashrc

Paste the function at the bottom of your “.bashrc” file.

manshell function in the gedit editor.

Save your changes and close the editor. Now, we need to read the “.bashrc” file to make the shell function active, so we type:

. .bashrc

Now, when we start a man page, it will be colorized in less:

man chmod

Running "man chmod" in a Bash terminal.

The man page opens with color highlighting.

In retrospect, yellow on magenta might not have been the best idea. Thankfully, you can tweak the color codes to your liking.

RELATED: How to Create Aliases and Shell Functions on Linux

It’s Not Just Pretty

It’s easy to scroll through a long man page and miss an important piece of information, like an option or parameter, because it’s lost in a sea of text.

Now, parameter and option names will be highlighted and much easier for you to spot.

Leave a Reply