In this tutorial I’ll show how to get some nicely colored man pages by adding several lines inside the .bashrc file, explaining what the code means and how it works.
Except for the eye-candy, colors may help when it comes to clarity, although some prefer the default monochrome approach. At the start of this article there is the actual code which can be copied and pasted inside the ~/.bashrc file, and which can be modified depending on each person’s preferences. If you don’t care about what it does you can just insert it in ~/.bashrc, restart your terminal and start reading some man pages to see how it looks (e.g. man man). After this part I tried to explain how this code works and how to modify it in order to get new ‘themes’.
For this tutorial we’ll be editing the ~/.bashrc file, which is a hidden file located in your home directory which gets read each time a new Bash instance starts up. Bash reads all the commands found in this file and executes them (hence the rc part, which stands for run commands).
We will edit this file and add some lines which specify certain colors for the $LESS_TERMCAP variables.
export LESS_TERMCAP_mb=$(printf '\e[01;31m') # enter blinking mode - red export LESS_TERMCAP_md=$(printf '\e[01;35m') # enter double-bright mode - bold, magenta export LESS_TERMCAP_me=$(printf '\e[0m') # turn off all appearance modes (mb, md, so, us) export LESS_TERMCAP_se=$(printf '\e[0m') # leave standout mode export LESS_TERMCAP_so=$(printf '\e[01;33m') # enter standout mode - yellow export LESS_TERMCAP_ue=$(printf '\e[0m') # leave underline mode export LESS_TERMCAP_us=$(printf '\e[04;36m') # enter underline mode - cyan
Don’t forget to reset your terminal after entering this code in order for the changes to take effect, e.g. type reset or exit and start up another shell.
This will mostly use magenta and cyan as the colors. Next, I’ll explain what these lines mean and how you can modify the colors.
As you can see, there are several variables which are assigned different values. As shown in the comments after the # sign, every one is used when needed. When text is in bold (double-bright mode), the formatting option is set to bold and the color magenta.
Let’s take, for example, the following line:
export LESS_TERMCAP_md=$(printf '\e[01;35m') # enter double-bright mode - bold, magenta
This line could be broken into this:
- the environment variable LESS_TERMCAP_md will be assigned the value to the right of the equal sign
- the right side says execute the command between the $( and ) characters, just like the older ` ` did
- printf is a command similar with C’s printf and means “print with format”. The characters between the double quotes specify a color and a font style (e.g. in this case, bold and magenta).
The part that is inside the double quotes are format specifiers like bold, regular, or color to be used. More on these can be found on Wikipedia, here.
The color codes are as follows:
- 30 – black
- 31 – red
- 32 – green
- 33 – orange
- 34 – blue
- 35 – magenta
- 36 – cyan
- 37 – white
Some other escape codes which you could use include:
- 0 – reset/normal
- 1 – bold
- 3 – italic/reversed
- 4 – underlined
- 5 – blink
You can check this by typing in a terminal something like:
printf '\e[31m' printf '\e[32m' printf '\e[37m'
So, if we have something like
printf '\e[01;33m'
it means enter bold and color yellow, according to the listing above.
export is a Bash built-in used to assign values to variables in such a manner that any subsequent application that runs in that shell will be aware of the variable’s value. If, for example, we would simply assign a value to a variable, say MYVAR=”this is my variable” and we would then issue echo $MYVAR, we would see that the variable’s value will be printed. However, try to make a simple script which would echo it, for example:
echo $MYVAR
And then run it e.g. bash myscript.sh – you will see that the value is lost, not visible in the script. So this is where export is useful, because it will make any further script or application “see” the variable. When we invoke the man command, it will need to see the values for our LESS_TERMCAP variables.
Here is another color scheme:
export LESS_TERMCAP_mb=$(printf '\e[01;31m') # enter blinking mode export LESS_TERMCAP_md=$(printf '\e[01;38;5;75m') # enter double-bright mode export LESS_TERMCAP_me=$(printf '\e[0m') # turn off all appearance modes (mb, md, so, us) export LESS_TERMCAP_se=$(printf '\e[0m') # leave standout mode export LESS_TERMCAP_so=$(printf '\e[01;33m') # enter standout mode export LESS_TERMCAP_ue=$(printf '\e[0m') # leave underline mode export LESS_TERMCAP_us=$(printf '\e[04;38;5;200m') # enter underline mode
Nice.
I didn’t bother with these way back when I wanted to have some colors in man.. just added
export PAGER=”most”
in .bashrc and that’s it.
Hey, Dan —
This is an excellent tutorial — thanks for writing it.
I don’t want to sound picky, but you might want to reconsider the title for it. I don’t know where in the world you might be, but “Colored Man” has racial overtones in the United States and elsewhere (like South Africa, too, where there’s also a history of racial segregation), and can be construed by the more “politically correct” crowd as racist.
I know what you mean by “Colored Man pages” and I’d be willing to bet you’re not a racist. As a newspaper editor for about 30 years, I come up against headlines like this all the time (“Police help dog bite victim,” or “Headless body found in topless bar”) and they’re always just gaffes to laugh about later. But I thought I’d give you heads-up on this.
Keep up the great work.
Dan:
It’s a nice article, thanks for writing it.
Even if unintended, the title can definitely be misconstrued. While it’s unfortunate that first reactions are associated with the racial interpretation, which obviously you do not mean based on the context of the article and an understanding of what a ‘man page’ is, you may consider weighing whether or not a very simple title change is a reasonable price to pay for not having all of the comment conversation detracting from the actual, excellent content of the article.
So just put “Man pages” as “Manpages” and everyone will be happy & fine.
Hi Larry, there are millions of issues like this in other countries, but do you see anyone from those countries making a big deal of that.
Such crap does not even come any where in the thoughts of the 99.99 people who read this blog.
You are just proving the US mindset. You have no special right to ‘correct’ other people (and you are under the assumption that others are thinking like you).
If this author was bad, I would just ignore him. Thats all there is to it.
Craciun Dan — That’s fine, and you could also go with “Coloring Man Pages . . . ” or something like that. Like Anon says, it’s a great tutorial and I’d hate to see the attention be drawn away from its goodness by the title.
Again, keep up the great work.
Excellent article, thanks! Very well explained, and fantastic level of English as a second language.
I note that no one was actually offended by your title, it was only a fear of offending that made you change it. I wish the PC crowd in the US would not export their hangups and shortcomings to the rest of the world! But I digress…
Looking forward to more articles of the same quality!
Cheers!
Good morning.
Nice work, Craciun Dan.
As depicted by Eman, a simple ‘export PAGER=”most”’ did the trick, with “Most” installed, of course.
Besides this, you can treat of “mostsh” lines in a file called .mostrc – this file will be scanned by MOST program.
My .mostc, as a tiny example, contains this:
export PAGER=”most”
color normal brightcyan black
color status white blue
color underline brightgreen black
color overstrike yellow black
…
Your care with explanation (did be dessication?) of what code does is technically flawless. Is that way we can help people to learn and like code.
Write ever for all us.
Morvan, User Linux #433640.
:-)
Hi
Nice tutorial with good explanation. Thanks for sharing it.
Though the white-majority of Australia put me in prison for 6-years, I’m a (PROUDLY !!) colored-man, but not optically color-blind. Back on topic, …
Color-blind people might prefer other color combinations. Your alternative coloring, we cannot see, unless we do it ourself.
Older people like myself are used to black text on whitish background. Some computer programmers allow for us dinosaurs. What are the codes for oldies like us?
Chief Information Officer, Retired (medical, 1984)
Australian Capital Territory
For whatever reasons the use of forward and backward single quotes given in the example were giving errors on my Ubuntu 14.04 system.
The single quotes are not easily visible as being forwards and backwards on this webpage, but once the text has been copied to a text file they can be easily identified as such.
Changing these single quotes to a standard single quote did the trick and now the copied text works fine on my system.
For clarity and as an example of what I’m trying to explain, if we take the first line given in the example and we change it from, lets call this case 01:
export LESS_TERMCAP_mb=$(printf ‘\e[01;31m’) # enter blinking mode – red
To this, lets call this case 02:
export LESS_TERMCAP_mb=$(printf ‘\e[01;31m’) # enter blinking mode – red
Notice the difference in the above use of single quotes from case 01 to case 02. Using the more generic single quote given in case 02 above for all the lines posted by the author of this article made the code error free and allowed it to work on my Ubuntu 14.04 setup.
Hope that helps someone out there. Its nice to have man pages that have color as it makes them easier to read and easier to find information more quickly.
Thanks for the tip. :)
BUG: All ‘ single quotations ‘ here are replaced by ‘ curly single quotations ’, which create a mess in the terminal. Please fix.
“The part that is inside the double quotes ”
Should be:
“The part that is inside the single quotes “