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
We will edit this file and add some lines which specify certain colors for the $LESS_TERMCAP variables.
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.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
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:
This line could be broken into this:export LESS_TERMCAP_md=$(printf '\e[01;35m') # enter double-bright mode - bold, magenta
- 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).
Color codes
The color codes are as follows:
- 30 – black
- 31 – red
- 32 – green
- 33 – orange
- 34 – blue
- 35 – magenta
- 36 – cyan
- 37 – white
- 0 – reset/normal
- 1 – bold
- 3 – italic/reversed
- 4 – underlined
- 5 – blink
printf '\e[31m'So, if we have something like printf ‘\e[01;33m’ it means enter bold and color yellow, according to the listing above.
printf '\e[32m'
printf '\e[37m'
What about ‘export’?
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:
#!/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
No comments:
Post a Comment