Advanced

Tutorial: Colored Man Pages & How It Works

Posted by Chris7mas on · 30 comments
TuxArena; By Craciun Dan
Mail this story
Print this story

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.

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’. The code

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. Explaining it

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. Color codes

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. What about ‘export’?

export is a Bash built-in used to assign values to variables in such a matter 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:

#!/bin/bash

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. More color schemes

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

References

http://www.ibm.com/developerworks/linux/library/l-bash/index... en.wikipedia.org/wiki/ANSI_escape_code http://www.gnu.org/software/termutils/manual/termcap-1.3/htm... https://wiki.archlinux.org/index.php/Man_Page http://tips4linux.com/color-man-pages-in-linux/

Full Story

  Nav
» Read more about: Story Type: Tutorial

» Get new stories by e-mail: nightly or as they are published

« Return to the newswire homepage

  Related stories
» More like this

Comments (30)

10 earlier comments in the full thread.

TA: It's an outdated term for African-American or black. It predates the civil rights movement and while it's not an epithet like the n-word it still can be seen as offensive or racist. Interestingly enough "people of color", when used to refer to any and all non-Caucasian people (and even dark skinned Caucasian people) is politically correct.
Colorful would have been a better word...
Every "objectionable" term of the kind originated as a politically correct alternative to some previous "prejudicial" term for the same. (I understand that sometimes such terms have even cycled between respectable and not).

Anybody remember the fuss a few years ago in California, about the terms "master" and "slave" being used to describe control circuitry, hard-drives, etc? Or the fuss about the political aid in Washington who described a budget as "niggardly"? -- and I had to turn off "safe search" to check which state it happened in.

Additionally, in the end, I can't help feeling somewhat suspicious of any term being considered "bad" or "pejorative", or not, depending chiefly on who is using it.

Sometimes the offended cries say much more about the offended against, than the offender. Sometimes the effort to avoid "giving offence" is itself offensive, and some people are determined to take offence, regardless of intent. You can't win. You might as well dispute "missing links" with a Creationist.

Anyone who takes offence at this particular title, is probably looking for a fight.
Quoting:Anyone who takes offence at this particular title, is probably looking for a fight.


And, of course, that never happens!
@ caitlyn, yes I am aware that coloured is or was used to describe a yank of African heritage. However it simply did not occur to me that the word had connotations when used in the context.

But, on rereading it I can see the humour of the juxtaposition of the words used.
I do remember the master/slave row. It was really stupid: the state government basically wanted hard drive manufacturers to stop using the terms, even though they were part of the IDE standard. What's bad is that there's nothing racist about the terms, or about slavery itself. Slavery has existed for thousands of years; the Egyptians, Greeks, Romans, and many other ancient civilizations had slaves, and they were usually people of the same or similar ethnicity (the Romans didn't have ships at the time capable of sailing down to central or south Africa, after all. The Greeks had a hard enough time just sailing across the Aegean Sea). I think people could become slaves by becoming too indebted even, though it was usually their immediate neighbors they had conquered in battle.

As for offense, anything you do is going to offend someone. For instance, I'm highly offended every time this site posts an article about Gnome3 or Unity that paints either one in a positive light. What is this site going to do about this? I'm offended!!! I'm also highly offended by Ken Hess articles!
Incidentally, the blogger (not a native English speaker, as it turns out) changed the title.
@lcafiero -

Nice to see a little sensitivity now and then when the problem can be easily rectified with no loss in meaning.
Indeed, dinotrac, indeed.
Hah, I saw this comment and clicked on it thinking dinotrac was criticising the appalling grammar :D
Here is earth shattering news: I agree with dino :) Yes, sensitivity is a good thing.
@caitlyn --

Ah ha! So YOU'RE the one to blame!

I just got off the phone with my mother who lives a bit north of Dallas, TX -- where 3 tornadoes have just made a little mosey on by.
LOL. Hope your mother and everyone else is OK.
@caitlyn --

Yeah, thanks. She's fine. The worst stuff managed to miss her. Not so sure about my mother-in-law's house, though.

She, fortunately, is visiting my sister-in-law's family in Florida, but her house was pretty close to one of the tornados' path. Still -- much better to be worrying about some bricks and furniture than a person.
Does anyone remember what the letters in the NAACP's name stand for?
Quoting:Does anyone remember what the letters in the NAACP's name stand for?


Good point, I guess with all the commotion going on lately we are becoming over sensitive or paranoid.

National Association for the Advancement of Colored People

http://www.naacp.org/

Still, colorful would have been a better choice.

Umm, The NAACP was founded in 1909, a time when "colored" was one heckuva lot nicer than many of the alternatives.
What Dino said...

Post a comment

A free account is needed to post. Write your comment here first: your text is kept in this browser while you sign up or log in, and is waiting for you when you come back.

Your message:
|||||
10
Markdown: **bold**, *italic*, [text](https://link), - lists, > quotes, $x^2$ for math. Formatting help

Sign up and post (it is free)  or  Log in and post