Customize man page colors with ‘less’ definitions

Man pages by default use less for displaying. I’ve used vim before to for colored text in man pages but something got bjorked in an update. To have color with man pages termcap will need to be invoked. Thanks to nico for the tip.

All that needs to be done is to export bold and underline values of termcap. Adding the values to the ~/.bashrc will make sure that they are always used:

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

And source the ~/.bashrc to have it work:

source ~/.bashrc

Some may notice I used Arch colors :) :


26 thoughts on “Customize man page colors with ‘less’ definitions

  1. Unsure. But termcap is limited and i dont’ think it has the ability to ‘recognize strings’ only predefined variables (like man-pages got). You can look at ‘man termcap’ to what it can do. A wrapper script possibly could be put around it but i think that would be alot, alot of work.

  2. Ahh, this is quite concerning and probably the source of the issue:

    %% env|grep LESS_TERMCAP

    any ideas?

  3. Hmm, this is a bit out there but I notice most people use \e to begin escape sequences before entering terminal colors. Possible try this?

  4. Those variables only *appear* to be emtpy, because they are entirely made of unprintable escape sequences.

    Try: env | grep LESS_TERMCAP | cat

    You still won’t see the codes, but you see the effect on the next line that’s printed out.

  5. Nice tip, works fine for me.
    I am interested in the colors though, cause i am on Arch :)
    Do you happen to have a corresponding (read: equally nice) color theme for your xterm and maybe vim ?


  6. That’s a bit in deliberation. I using yakuake, and using a theme based on Konsole, Oxygen theme:

    The values I’m using:

    color0 181C20 color1 A44245 color2 00BF00 color3 BBAD5B color4 2B76C7 color5 8B56A3 color6 5BCEE4 color7 D4D7D0 color8 A44245 color9 A44245 color10 37A42B color11 C4C29B color12 2B76C7 color13 8B56A3 color14 71AAB3 color15 EAEAEA

    As for vim there’s alot to choose but I like how the Oxygen scheme looks in it. For more basic tones you might want to look at brian’s Gentooish vim scheme though:

  7. for mac os x this is what i had to use, as it appears a less advanced ansi color interpretation:

    export LESS_TERMCAP_mb=$’\E[01;31m’ # begin blinking
    export LESS_TERMCAP_md=$’\E[01;31m’ # begin bold
    export LESS_TERMCAP_me=$’\E[0m’ # end mode
    export LESS_TERMCAP_se=$’\E[0m’ # end standout-mode
    export LESS_TERMCAP_so=$’\E[01;44m’ # begin standout-mode – info
    export LESS_TERMCAP_ue=$’\E[0m’ # end underline
    export LESS_TERMCAP_us=$’\E[32m’ # begin underline

  8. I see a few issues with this approach. If you login to your account from different terminals, the same escape sequences will be sent in all cases. There’s no way to personalize this for different terminals. (Also, it uses an undocumented feature of less, which may change or disappear. These LESS_TERMCAP_xxx environment variables were intended to make it easy to cause escape sequences to become visible, to aid in debugging.)

    I think a better approach is to personalize your termcap/terminfo database. On a Fedora systems, for example, I can do

    echo ‘xterm-mycolors, bold=\E[38;5;246m, sgr0=\E[0m, use=xterm,’ >myterm.ti

    tic myterm.ti

    export TERM=xterm-mycolors

    This not only achieves the same thing in less without using undocumented features, it makes bold text be the desired color in all programs that use termcap/terminfo.


  9. To see the normally-invisible escape sequences in the LESS_TERMCAP_* variables, try `cat -v`:

    $ env | grep ^LESS_TERMCAP | cat -v

    You can also use less itself, as long as its ‘raw control chars’ mode is disabled, i.e. `env | grep ^LESS_TERMCAP | less -+r`.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s