We're an ISO27001:2013 Certified Supplier

blog-post-featured-image

Colourful syntax highlighting in editors has been around since at least the 1980s, and has made writing (and reading) code much easier and less error-prone.

Most of us, though, continue to view man pages in monochrome, despite all of us (surely?) having colour terminals or emulators. Adding colour to man pages may not have quite the same impact as it does with editors, but it does make man pages easier to read (and prettier!).

Pagers

By default, man pages are displayed via less, which in turn uses termcap to work out how to display text. There are two ways of displaying man pages in colour: either change the termcap information, or use a different pager other than less.

Changing termcap Information

There are a number of environment variables that can define the escape sequences used by termcap. For example, LESS_TERMCAP_md defines the escape sequence used to start bold text, and we can override that to display bold text in, say, red:

export LESS_TERMCAP_mb=$'\E[01;31m'

That will show man page headings in red. There’s a useful colour chart available online so that you can pick the perfect foreground and background colours.

The environment variables you need are:

LESS_TERMCAP_mb    # begin blinking
LESS_TERMCAP_md    # begin bold
LESS_TERMCAP_me    # end mode
LESS_TERMCAP_se    # end standout-mode
LESS_TERMCAP_so    # begin standout-mode
LESS_TERMCAP_ue    # end underline
LESS_TERMCAP_us    # begin underline

The “end mode” escape sequences reset the output, and typically is set to '\e[0m'. By way of example, here’s a complete set of commands that could be put into a ~/.bashrc file:

export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;31m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;44;37m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;33m'

That will put headings in red, keywords in yellow, and have a white-on-blue prompt line:

manpage colours

Using A Different Pager

If all that seems a little messy, there is another way. One alternative pager available on many Linux distributions is most. We can tell man to use most as its pager, and we’ll automatically get a coloured output. Once most is installed, these commands:

$ export PAGER=most
$ man bash

…will give this output:

man page with most

Was This Linux Tip Helpful?

Let us know in the comments below.