Linux: Colored Man Pages

Linux is my favorite operating system, especially gentoo distro. The command line is an awesome tool, but sometimes I’m missing the colors on black&white terminal. Yes, you can simply colorize the command ls or grep just adding the line

alias ls='ls --color=auto'

but how about the man pages ?

Man pages by default use less for displaying. So let’s go configure LESS to export bold and underline values of termcap:

export LESS_TERMCAP_mb=$'\E[01;31m'          # begin blinking
export LESS_TERMCAP_md=$'\E[01;38;5;74m'  # begin bold
export LESS_TERMCAP_so=$'\E[38;5;246m'      # begin standout-mode - info box
export LESS_TERMCAP_us=$'\E[04;38;5;146m' # begin underline
export LESS_TERMCAP_me=$'\E[0m'                 # end mode
export LESS_TERMCAP_se=$'\E[0m'                  # end standout-mode
export LESS_TERMCAP_ue=$'\E[0m'                  # end underline

Add the shell variables “LESS_TERMCAP_*” to gentoo configuration defined in /etc/env.d/70less or ~/.bashrc in general to work.

The ANSI escape sequence ’38;5;74m’ define xterm-256 foreground color with last number as a color index 0..255. Customize the colors as you like according to ANSI escape sequence.

Check if ANSI “color” escape sequences is enabled with parameter “-R” to LESS command:

$ env | grep LESS=
LESS=-R -M --shift 5

Check if man pages are rendered with color output. If not modify /etc/man.conf' and add switch ‘-c’ to NROFF configuration:

NROFF           /usr/bin/nroff -mandoc -c

Finally, you now see colored 'man ls' output:

       ls - list directory contents

       ls [ OPTION ]... [ FILE ]...

       List information about the FILEs (the current directory by default).  Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --all
              do not ignore entries starting with .

       -A, --almost-all
              do not list implied . and ..

              with -l, print the author of each file

       -b, --escape

Leave a Reply

Your email address will not be published. Required fields are marked *