Make your man command output colored!

CaffeineAddict

Well-Known Member
Joined
Jan 21, 2024
Messages
500
Reaction score
314
Credits
4,185
I disliked so much to type man whatever into terminal because entire text is white & black like TV's from 80's so instead I used to read man pages online.
But that changed, here is what I did:

Bash:
sudo apt install terminfo

Append the following code to the end of your ~/.bashrc

Bash:
# Bold text
export LESS_TERMCAP_md=$(tput bold; tput setaf 2) # Green
# End all mode like so, us, mb, md, and mr
export LESS_TERMCAP_me=$(tput sgr0)
# Start underlining
export LESS_TERMCAP_us=$(tput smul; tput setaf 4) # Blue
# End underlining
export LESS_TERMCAP_ue=$(tput rmul; tput sgr0)

Then fire up new terminal and type for instance man fstab and here is how it looks now, it's colored!


man.png
 


That's fancier and more involved than the way I know. Neat! Thanks for sharing.
 
Just in case anyone wonders how this works...

Sample:
Bash:
export LESS_TERMCAP_md=$(tput bold; tput setaf 2)

Breakdown:
Markdown (GitHub flavored):
- `export` is used to export `LESS_TERMCAP_*` variable for specific capability
- `LESS_TERMCAP_*` modifies `less` which is used by `man` to render manuals
- `md` suffix is a `termcap` capability specifying what to modify, `md` means to modify bold test
  - For other capabilities to append to `LESS_TERMCAP_` see `man termcap`
- `$()` starts `terminfo` block which specifies `terminfo` capabilities and values
  - `tput` initializes a terminal or query terminfo database, see `man tput`
  - `setaf` is `terminfo` capability which sets foreground color, see `man terminfo`
  - `2` is green color, for color reference see `Color Handling` section in `man terminfo`

Color reference:

Markdown (GitHub flavored):
| Color   | Value  |
| --------| ------ |
| black   |   0    |
| red     |   1    |
| green   |   2    |
| yellow  |   3    |
| blue    |   4    |
| magenta |   5    |
| cyan    |   6    |
| white   |   7    |

Reference:

man terminfo
man termcap
man tput

Benefits of this approach:

1. termcap is obsolete and terminfo is it's newer replacement so you'll be using most recent method and software.
2. You don't need to install most and set pager to most because man will continue to use less except with colors.
 

Members online


Top