Colors in Man Pages
Solution 1
You need to use the termcap(5)
feature. The man page on some Unices says this tool is obsolete and to use terminfo
, but it's still available on others (and terminfo
is more complicated).
More importantly, less
uses termcap
.
Setting colors for less
I do the following so that less
and man
(which uses less
) will have color:
$ cat ~/.LESS_TERMCAP
export LESS_TERMCAP_mb=$(tput bold; tput setaf 2) # green
export LESS_TERMCAP_md=$(tput bold; tput setaf 6) # cyan
export LESS_TERMCAP_me=$(tput sgr0)
export LESS_TERMCAP_so=$(tput bold; tput setaf 3; tput setab 4) # yellow on blue
export LESS_TERMCAP_se=$(tput rmso; tput sgr0)
export LESS_TERMCAP_us=$(tput smul; tput bold; tput setaf 7) # white
export LESS_TERMCAP_ue=$(tput rmul; tput sgr0)
export LESS_TERMCAP_mr=$(tput rev)
export LESS_TERMCAP_mh=$(tput dim)
export LESS_TERMCAP_ZN=$(tput ssubm)
export LESS_TERMCAP_ZV=$(tput rsubm)
export LESS_TERMCAP_ZO=$(tput ssupm)
export LESS_TERMCAP_ZW=$(tput rsupm)
export GROFF_NO_SGR=1 # For Konsole and Gnome-terminal
And then in my ~/.bashrc
, I do this:
# Get color support for 'less'
export LESS="--RAW-CONTROL-CHARS"
# Use colors for less, man, etc.
[[ -f ~/.LESS_TERMCAP ]] && . ~/.LESS_TERMCAP
NOTE: See Documentation on LESS_TERMCAP_*
variables? for how this works.
The final result
Solution 2
The default underlines hurt my eyes. This setup greatly improves my man
page reading:
Add the following in your `~.bashrc':
# Have less display colours
# from: https://wiki.archlinux.org/index.php/Color_output_in_console#man
export LESS_TERMCAP_mb=$'\e[1;31m' # begin bold
export LESS_TERMCAP_md=$'\e[1;33m' # begin blink
export LESS_TERMCAP_so=$'\e[01;44;37m' # begin reverse video
export LESS_TERMCAP_us=$'\e[01;37m' # begin underline
export LESS_TERMCAP_me=$'\e[0m' # reset bold/blink
export LESS_TERMCAP_se=$'\e[0m' # reset reverse video
export LESS_TERMCAP_ue=$'\e[0m' # reset underline
export GROFF_NO_SGR=1 # for konsole and gnome-terminal
For the win, combine with export MANPAGER='less -s -M +Gg'
(source) to display your percentage into the document.
Solution 3
You can solve this issue by using a different pager, for example most
. man
will actually use the program specified in the PAGER
environment variable. From the man(1) man page:
MANPAGER, PAGER
If $MANPAGER or $PAGER is set ($MANPAGER is used in preference), its value is used as the name of the program used to display the manual page. By default, pager -s is used.
The value may be a simple command name or a command with arguments, and may use shell quoting (backslashes, single quotes, or double quotes).
It may not use pipes to connect multiple commands; if you need that, use a wrapper script, which may take the file to display either as an argument or on standard input.
If most
is installed on your system, try this, before launching man
:
export PAGER=most
Solution 4
If you use Oh My Zsh, you can add colored-man-pages
to the plugins
array in your .zshrc
file.
Solution 5
Save 'most' persistently
Open Terminal (Ctrl+Alt+T)
-
Install 'most'.
sudo apt-get install most
-
edit .bashrc , type:
nano ~/.bashrc
-
Add these lines:
# color man-pages persistently export PAGER='most'
-
Save
(Ctrl+O) -> Enter -> (Ctrl+X)
-
Refresh
source ~/.bashrc
-
Test
man ln
This also works in xterm.
Related videos on Youtube
xenoterracide
Former Linux System Administrator, now full time Java Software Engineer.
Updated on September 17, 2022Comments
-
xenoterracide over 1 year
When I look at a man page in my 'console' (not an
xterm
) I see some coloration, but I don't get this in myxterm
's (e.g.konsole
) is there any way I can enable this? hopefully a fairly simple solution? -
Michael Mrozek over 13 yearsIt helps if you summarize the source here, so people can see what it says without having to click through (and in case the site ever goes down)
-
Mark Norgren over 13 yearsPlease provide answers, not just links to answers. It's very good to provide links which support your answer, however. Imagine I was Googling for this question--- a good answer at StackExchange will quickly find it's way to the first page of results.
-
Kent Fredric about 13 yearsI believe the reason this works the way it does, is because 'console' translates 'underline' into colour, where-as X11 stuff supports underlines. You can test this theory by typing
echo -e "\e[04mhello world\e[0m"
in both the console and your X11 terminal and seeing the difference. So this above hack abuses termcap to lie to LESS about what codes it needs to emit for bold/underline and forces it to produce colour escape codes instead. -
enzotib over 12 years
most
should be installed, for this to work. -
celtschk about 10 yearsIndeed, the translation of underline into blue has historic reasons, going back to the text modes of the original PC graphics adapters MDA and CGA (actually the CGA text modes are still available to date). Those graphics adapters stored two bytes per character: One holding the ASCII code, one holding the attributes. The MDA interpreted the attribute byte as combinations of underline, bright, blinking and inverse, while the CGA interpreted that byte as foreground and background colour. And it happened that the MDA attribute for underline equalled the CGA attribute for blue on black.
-
CMCDragonkai about 8 yearsThis doesn't work in Konsole or Gnome-terminal. I set them, and the only colour change is for the cursor and the status page. I turns out I need:
export GROFF_NO_SGR=1
. -
Mark Norgren about 8 yearsWell, this used to work in Gnome Terminal, but it's been a while since I wrong these instructions so it's possible that something has changed.
-
farzan almost 8 yearsThe link is dead.
-
Kaushal Modi over 7 yearsFound this
mostlike.txt
online. -
Kaushal Modi over 7 yearsWorks for me on
tmux
inuxterm
. Thanks! -
Tom Hale over 7 yearsWhat does the output look like?
-
Marius over 7 yearsIt's intended to show bold as bright-red, and underline as green. But I wouldn't use it as written.
-
Tom Hale over 7 yearsThat's a lot of sub-shells created. What is the advantage over this simpler approach?
-
Ben over 7 yearsAs @CMCDragonkai mentioned in a comment on another answer, this requires
export GROFF_NO_SGR=1
to work on some terminal emulators. -
oHo almost 7 yearsPlease update your answer to advice setting
export GROFF_NO_SGR=1
as mentioned by @CMCDragonkai unless your answer does not work on konsole, gnome-terminal, terminology... Cheers -
Mateen Ulhaq almost 6 years
most
has strange keybindings... which are not configurable. -
Mark G. almost 6 years@TomHale, mainly cross-system/terminal compatibility (and the ability to share the script with others in a way that's unambiguous). If you ever find yourself working on a box that uses different control codes, and you scp/rsync your shell profile over to it, when the control codes are all hard-coded like in that example, they may not work as expected on the destination machine, and you could end up with garbled output. Of course, if the script will only ever run on one or two known machines/terminal-emus, then the hard-coded approach is just fine.
-
Will over 5 yearsto avoid opening nano, you can append to bashrc from the terminal: sudo apt install most; echo "export PAGER='most'" >> ~/.bashrc; source ~/.bashrc
-
MikeyE over 5 yearsFor the win!! I love the % display
export MANPAGER='less -s -M +Gg'
adds. -
Terry Wang almost 4 yearsThis one works better than the Arch Linux Wiki implementation, reusable, nice. Thanks!
-
Terry Wang almost 4 yearsJust to added to the last comment, it works along with Arch Wiki way very well, even better. Tested on Fedora 32, Ubuntu 20.04 and Arch Linux / Manjaro ;-)
-
alper almost 4 yearsusing
colored-man-pages
freezes my man page during search -
tremby almost 4 yearsYou should tell its developers about that.
-
alper almost 4 years
tic: Can't open xterm-yellow.ti
-
alper almost 4 yearsI pasted into a file called
xterm-yellow.ti
but now I get following error"xterm-yellow.ti", line 8, col 2, terminal 'xterm-yellow': Illegal character (expected alphanumeric or @%&*!#) - '^?'
-
alper almost 4 yearsDuring search this does not highlights the selected work in different color, is it possible to have?
-
Shane Bishop over 3 yearsThis has the disadvantage of setting your pager to
most
for all uses of pagers. It would be better to useexport MANPAGER='most'
if you only want to change your pager for man pages. -
Admin almost 2 yearsRegarding
ZN
andssubm
, etc: less doesn't useZN
, and the other subscript-related capabilities (which are defined in themintty
description). -
Admin almost 2 yearsAnd less doesn't actually use termcap on any system you're likely to be using. Just the termcap interface to terminfo.