A command prompt in a terminal window on a Linux computer.
Fatmawati Achmad Zaenuri/Shutterstock

If you want color highlighting in your man pages similar to the syntax highlighting in an editor, there are two simple ways you can achieve it. We’ll show you both!

Color Highlighting

Color highlighting makes things easier to read. It can make details pop, so you don’t skim past and miss them. Most modern editors support syntax highlighting, which uses color to identify and differentiate between different elements of a programming language. Reserved words, variables, strings, and numbers are all colorized to make it easier to visually parse a page or function of code.

Having this feature in the Linux man pages would be extremely helpful. Despite favoring brevity, some man pages are big, dense, and difficult to get through. Anything that makes it easier to visually navigate them is a good thing.

We’re going to describe two ways you can get a colorized effect in man pages. One involves using a different pager to display them, while the other requires passing a bunch of parameters to less at run time. The neatest way to do that is to create a shell function.

The most Pager

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

To install most on Ubuntu, use this command:

sudo apt-get install most

sudo apt-get install most in a terminal window.

To install most on Fedora, type:

sudo dnf install most

sudo dnf install most in a terminal window.

To install most on Manjaro, you type:

sudo pacman -Syu most

sudo pacman -Syu most in a terminal window.

Set most as the Default Pager

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

We type the following:

export PAGER=“most”

export PAGER=“most” in a terminal window.

This only works until you close the terminal window, though. To make this change permanent, we have to add it to the “.bashrc” file (we’ll make it the last line in the file):

gedit .bashrc

gedit .bashrc in a terminal window.

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

.bashrc in a the gedit editor.

To make the contents of the modified “.bashrc” file active, we close and reopen the terminal window.

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

We type the following:

. .bashrc

. .bashrc in a terminal window.

Color man Pages

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

man grep

man grep in a terminal window.

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

man page with color highlighting.

Scroll down, and you’ll see how the different elements of the page are colorized.

A section of a man page with color highlighting in a terminal window.

Using most is very similar to using less, but there are some differences. Press H in  most to see a list of keybindings and their functions.

The most pager help screen in a terminal window.

Using Color with less

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

This method uses the American National Standards Institute (ANSI) color codes to control the onscreen effects associated with the old and mostly defunct termcap settings.

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

Here are the definitions of the less termcap settings:

  • LESS_TERMCAP_md: Start bold effect (double-bright).
  • LESS_TERMCAP_me: Stop bold effect.
  • LESS_TERMCAP_us: Start underline effect.
  • LESS_TERMCAP_ue: Stop underline effect.
  • LESS_TERMCAP_so: Start stand-out effect (similar to reverse text).
  • LESS_TERMCAP_se: Stop stand-out effect (similar to reverse text).

Again, we’ll set these to control color combinations using the American National Standard Institute (ANSI) color codes.

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

  • The “\e” at the beginning 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 be actioned.
  • The numbers between the “[” 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

gedit .bashrc in a terminal window.

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

. .bashrc in a terminal window.

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.

A colorized man page in less in a terminal window.

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.

Dave McKay Dave McKay
Dave McKay first used computers when punched paper tape was in vogue, and he has been programming ever since. After over 30 years in the IT industry, he is now a full-time technology journalist. During his career, he has worked as a freelance programmer, manager of an international software development team, an IT services project manager, and, most recently, as a Data Protection Officer. Dave is a Linux evangelist and open source advocate.
Read Full Bio »

The above article may contain affiliate links, which help support How-To Geek.