When I look at a man page in my 'console' (not an xterm
) I see some coloration, but I don't get this in my xterm
's (e.g. konsole
) is there any way I can enable this? hopefully a fairly simple solution?
-
Wow, I just realized that this is one of the earliest questions on this site (119th), and it was asked on the first day of this site. It's kinda amazing and sad that the accepted solution from 12 years ago is still the best solution, and there aren't better alternatives!– John RedNov 7, 2022 at 22:23
-
Note that man pages are basically just plain text with some bold and underline added. There are no hyperlinks (at least not explicitly). So you don't need colors to see all the features man pages are written with. Not to say there is anything wrong at all with colorizing man pages. It's just an aesthetic choice.– cambunctiousJun 8 at 13:25
8 Answers
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
-
10I 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. Feb 15, 2011 at 12:03 -
10Indeed, 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.– celtschkMay 8, 2014 at 18:39
-
5This 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
. May 4, 2016 at 7:11 -
2That's a lot of sub-shells created. What is the advantage over this simpler approach?– Tom HaleJan 20, 2017 at 7:34
-
1@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.– Mark G.Jul 28, 2018 at 19:07
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.
-
4As @CMCDragonkai mentioned in a comment on another answer, this requires
export GROFF_NO_SGR=1
to work on some terminal emulators.– BenJan 22, 2017 at 22:58 -
2For the win!! I love the % display
export MANPAGER='less -s -M +Gg'
adds.– MikeyEOct 11, 2018 at 14:00 -
During search this does not highlights the selected work in different color, is it possible to have?– alperJul 5, 2020 at 11:49
-
How can I also change the color for the echo area as well (bottom section?) Mine's background is purple and its unreadable .– alperJul 5, 2020 at 11:58
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
-
4
-
8
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.
-
to avoid opening nano, you can append to bashrc from the terminal: sudo apt install most; echo "export PAGER='most'" >> ~/.bashrc; source ~/.bashrc– WillSep 7, 2018 at 19:14
-
3This 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. Jan 9, 2021 at 19:17
On Linux, you could try the following MANPAGER
settings [1]. The second one needs bat
.
MANPAGER="less -R --use-color -Dd+r -Du+b"
or
MANPAGER="sh -c 'col -bx | bat -l man -p'"
On Gentoo Linux, you could also try this, which needs app-text/manpager
MANPAGER=manpager
(I didn't read the code of manpager
, I guess it is just a wrapper of the command less
and the environment variables LESS_TERMCAP_??
).
[1] https://wiki.gentoo.org/wiki/Man_page#Color_for_man_pages
-
I like this solution the most (specifically the first suggestion). It seems to be cleaner and much simpler than messing with termcap sequences in
LESS_TERMCAP_*
environment variables. Moreover it allows to combine bold and underlined text with colors (instead of replacing them). Are there any downsides? Jul 6 at 22:41
Taking dirtybit’s answer, I wanted to change the color of highlighted search results. This is called "standout mode", here is an example file "xterm-yellow.ti":
xterm-yellow|yellow standout mode,
# exit standout mode
rmso=\e[m,
# begin standout mode
smso=\e[5;30;43m,
# similar terminal
use=xterm,
Compile and install the file:
tic xterm-yellow.ti
Add line to "~/.profile" or similar:
TERM=xterm-yellow
-
-
I 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 @%&*!#) - '^?'
– alperJul 5, 2020 at 8:21
Taken from http://nion.modprobe.de/blog/archives/569-colored-manpages.html
Excerpt:
$ mkdir ~/.terminfo/ && cd ~/.terminfo
Now get the terminfo
description (save the following as mostlike.txt):
# Reconstructed via infocmp from file: /usr/share/terminfo/x/xterm-pcolor
mostlike|manpages with color looking like most,
am, hs, km, mir, msgr, xenl,
cols#80, it#8, lines#24, wsl#40,
acsc=``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~,
bel=^G, bold=\E[1m\E[31m, clear=\E[H\E[2J, cr=^M,
csr=\E[%i%p1%d;%p2%dr, cub=\E[%p1%dD, cub1=^H,
cud=\E[%p1%dB, cud1=^J, cuf=\E[%p1%dC, cuf1=\E[C,
cup=\E[%i%p1%d;%p2%dH, cuu=\E[%p1%dA, cuu1=\E[A,
dch=\E[%p1%dP, dch1=\E[P, dl=\E[%p1%dM, dl1=\E[M,
dsl=\E]0;\007, ed=\E[J, el=\E[K, enacs=\E)0, fsl=^G,
home=\E[H, ht=^I, hts=\EH, il=\E[%p1%dL, il1=\E[L, ind=^J,
is2=\E7\E[r\E[m\E[?7h\E[?1;3;4;6l\E[4l\E8\E>, kbs=^H,
kcub1=\EOD, kcud1=\EOB, kcuf1=\EOC, kcuu1=\EOA,
kdch1=\E[3~, kf1=\E[11~, kf10=\E[21~, kf11=\E[23~,
kf12=\E[24~, kf13=\E[25~, kf14=\E[26~, kf15=\E[28~,
kf16=\E[29~, kf17=\E[31~, kf18=\E[32~, kf19=\E[33~,
kf2=\E[12~, kf20=\E[34~, kf3=\E[13~, kf4=\E[14~,
kf5=\E[15~, kf6=\E[17~, kf7=\E[18~, kf8=\E[19~, kf9=\E[20~,
kfnd=\E[1~, kich1=\E[2~, kmous=\E[M, knp=\E[6~, kpp=\E[5~,
kslt=\E[4~, rc=\E8, rev=\E[7m\E[34m, ri=\EM, rmacs=^O,
rmcup=\E[2J\E[?47l\E8, rmir=\E[4l, rmkx=\E[?1l\E>,
rmso=\E[m, rmul=\E[m,
rs2=\E7\E[r\E8\E[m\E[?7h\E[?1;3;4;6l\E[4l\E>, sc=\E7,
sgr0=\E[m, smacs=^N, smcup=\E7\E[?47h, smir=\E[4h,
smkx=\E[?1h\E=, smso=\E[1;30m\E[47m, smul=\E[32m,
tbc=\E[3g, tsl=\E]0;, u6=\E[%i%d;%dR, u7=\E[6n,
u8=\E[?1;2c, u9=\E[c,
Now compile it using tic
(the terminfo entry-description compiler):
$ tic mostlike.txt
(You may want to delete the mostlike.txt
file after compiling.)
And then just define an alias in the *rc
file of your favorite shell.
$ alias man="TERMINFO=~/.terminfo/ LESS=C TERM=mostlike PAGER=less man"
If you want to modify the terminfo file, use infocmp mostlike
to get the content of it later.
-
11It 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) Aug 10, 2010 at 20:57
-
3Please 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. Aug 10, 2010 at 21:27
-
-
-