97

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?

Anthon
  • 79,293
xenoterracide
  • 59,188
  • 74
  • 187
  • 252
  • 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 Red Nov 07 '22 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. – cambunctious Jun 08 '23 at 13:25

8 Answers8

106

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

    ss of man page

Stefan Lasiewski
  • 19,754
  • 24
  • 70
  • 85
  • 11
    I 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. – Kent Fredric Feb 15 '11 at 12:03
  • 10
    Indeed, 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. – celtschk May 08 '14 at 18:39
  • 6
    This 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. – CMCDragonkai May 04 '16 at 07:11
  • Well, 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. – Stefan Lasiewski May 04 '16 at 17:11
  • Works for me on tmux in uxterm. Thanks! – Kaushal Modi Sep 07 '16 at 15:32
  • 2
    That's a lot of sub-shells created. What is the advantage over this simpler approach? – Tom Hale Jan 20 '17 at 07:34
  • 1
    Please 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 – oHo Jul 08 '17 at 21:22
  • 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 '18 at 19:07
  • This one works better than the Arch Linux Wiki implementation, reusable, nice. Thanks! – Terry Wang May 13 '20 at 08:20
  • Just 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 ;-) – Terry Wang May 14 '20 at 03:32
  • Regarding ZN and ssubm, etc: less doesn't use ZN, and the other subscript-related capabilities (which are defined in the mintty description). – Thomas Dickey Jul 04 '22 at 18:03
  • And less doesn't actually *use* termcap on any system you're likely to be using. Just the termcap interface to terminfo. – Thomas Dickey Jul 04 '22 at 18:04
  • 2
    Isn't this better: MANPAGER="less -R --use-color -Dd+r -Du+b"? It turns bold text to red color and underlined text to blue color. It's suggested in Bruce's answer. It seems to be cleaner and much simpler than messing with the termcap sequences. – David Ferenczy Rogožan Jul 06 '23 at 22:45
29

The default underlines hurt my eyes. This setup greatly improves my man page reading:

Coloured man page example

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.

Tom Hale
  • 30,455
  • 5
    As @CMCDragonkai mentioned in a comment on another answer, this requires export GROFF_NO_SGR=1 to work on some terminal emulators. – Ben Jan 22 '17 at 22:58
  • 2
    For the win!! I love the % display export MANPAGER='less -s -M +Gg' adds. – MikeyE Oct 11 '18 at 14:00
  • During search this does not highlights the selected work in different color, is it possible to have? – alper Jul 05 '20 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 . – alper Jul 05 '20 at 11:58
18

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
0xYUANTI
  • 4,366
13

If you use Oh My Zsh, you can add colored-man-pages to the plugins array in your .zshrc file.

tremby
  • 523
  • 7
  • 15
5

Save 'most' persistently

Open Terminal (Ctrl+Alt+T)

  1. Install 'most'.

    sudo apt-get install most
    
  2. edit .bashrc , type:

    nano ~/.bashrc
    
  3. Add these lines:

    # color man-pages persistently
    export PAGER='most'
    
  4. Save

    (Ctrl+O) -> Enter -> (Ctrl+X)

  5. Refresh

    source ~/.bashrc
    
  6. 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
    
    – Will Sep 07 '18 at 19:14
  • 3
    This has the disadvantage of setting your pager to most for all uses of pagers. It would be better to use export MANPAGER='most' if you only want to change your pager for man pages. – Shane Bishop Jan 09 '21 at 19:17
5

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

Bruce
  • 1,241
  • 1
    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? – David Ferenczy Rogožan Jul 06 '23 at 22:41
  • I, too, like the 1st suggestion here more than the presently most-upvoted answer from 2010 (which I've been using for years) because it doesn't need subshells and its effects are limited solely to man(1) pages via the MANPAGER env var. Upvoted. Thanks for cluing me into the existence of less(1)' -Dx option! For posterity: MANPAGER='less --use-color -Dd+c -DPYb -DSYb' more-or-less re-creates the colors of the 2010 answer for those who prefer its "historical" MDA/CGA-inspired scheme. Even more spartanly: MANPAGER='less -Duc' can make X11 and console man pages more visually consistent. :-) – Mark G. Dec 26 '23 at 23:48
1

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
Zombo
  • 1
  • 5
  • 44
  • 63
  • tic: Can't open xterm-yellow.ti – alper Jul 05 '20 at 00:06
  • 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 @%&*!#) - '^?' – alper Jul 05 '20 at 08:21
0

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.

dirtybit
  • 330