I just added this to my .bashrc
to get colorized output with less
:
# Colorize less man pages.
export LESS_TERMCAP_md=$'\e[01;34m'
export LESS_TERMCAP_us=$'\e[01;33m'
export LESS_TERMCAP_so=$'\e[01;44;37m'
export LESS_TERMCAP_mb=$'\e[01;31m'
export LESS_TERMCAP_mr=$'\e[01;35m'
export LESS_TERMCAP_me=$'\e[00m'
export LESS_TERMCAP_ue=$'\e[00m'
export LESS_TERMCAP_se=$'\e[00m'
… and now all of a sudden certain commands (seems to be related to displaying environment variables) produce color output that matches these new settings. Am I escaping wrong? Or is this intentional behavior? I tried a few other escape variations, but they didn't work with less
.
For example, here is a screenshot of an env
command.
php -i
also has colorized output, but only on the environment variables section.
Best Answer
This is normal behavior. These environment variables contain escape sequences that cause the terminal to change its foreground color. You get the same visual effect when any program outputs them, be it
less
orenv
.These variables need to contain the actual escape characters, less doesn't do any postprocessing on them.
Normally you can put less configuration variables in your lesskey file, but this doesn't work for the
LESS_TERMCAP_xx
variables, because less reads them before it reads the lesskey file (as of less 444). So you have no choice but to put them in the environment.If you want these variables to apply only to
man
and not to other uses ofless
, you can use an alias forman
that sets thePAGER
variable to a wrapper script that sets the environment variables.(Alternatively, use
#!/bin/bash
on the first line and you can use the#'\e'
syntax to get an escape character. On systems where/bin/sh
is dash, using/bin/sh
is very slightly faster, although it may not be noticeable in practice.)Call this script
less-color
, and addalias man='PAGER=less-color man'
to your~/.bashrc
or~/.zshrc
. On some systems, instead of creating an alias, you can tellman
to use a different pager by setting theMANPAGER
environment variable:export MANPAGER=less-color
in your~/.profile
.