Last Updated: February 25, 2016
·
1.717K
· supersymmetry

Coloring and retain your terminal 'man' output

Since there are many ways one can color their virtual terminal and there are many possible combinations of them, I'll limit the scope of this pro-tip article to your basic X11 terminal emulator (xterm, urxvt, gnome-terminal, terminator and what not) using less as a pager for displaying the man pages:

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

Also, you will need to use the following alias to substitute the 'man' command:

alias man='man -P less'

You can add the above exports to your .bashrc or .zshrc file but if you like clean separation like with .bash_aliases and what not, you can source them from within the run-command files like:

[[ -f "${HOME}/.colors" ]] && . "${HOME}/.colors"

It took me a while before I got aware this color output was even possible, but it's fast becoming one of my personal favorite tweaks. This way I start to like the impossible man pages again because the variables, switches and so on are now easier to distinguish from regular text. This aids in quick glances over the file. Also note that you can use /-switch while in less to search for any options you need to read up on quickly.

The final tweak that comes in handy with the color output and quick search in less:

export LESS="-X"

Which is the means to retain output on screen after you exit using 'q'. This will keep the manual page information straight in sight for you to read, copy/paste and/or type. Make your pick and enjoy :)

P.s. here's another snippet with different coloring