86

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?

0

8 Answers 8

97

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

10
  • 10
    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. Feb 15, 2011 at 12:03
  • 9
    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 8, 2014 at 18:39
  • 4
    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. May 4, 2016 at 7:11
  • 2
    That's a lot of sub-shells created. What is the advantage over this simpler approach?
    – Tom Hale
    Jan 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
24

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.

4
  • 3
    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, 2017 at 22:58
  • 1
    For the win!! I love the % display export MANPAGER='less -s -M +Gg' adds.
    – MikeyE
    Oct 11, 2018 at 14:00
  • During search this does not highlights the selected work in different color, is it possible to have?
    – alper
    Jul 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 .
    – alper
    Jul 5, 2020 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
2
  • 4
    most should be installed, for this to work.
    – enzotib
    Sep 14, 2011 at 7:41
  • 6
    most has strange keybindings... which are not configurable. Jun 18, 2018 at 8:19
11

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

2
  • using colored-man-pages freezes my man page during search
    – alper
    Jul 3, 2020 at 12:40
  • You should tell its developers about that.
    – tremby
    Jul 5, 2020 at 0:04
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.

2
  • 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 7, 2018 at 19:14
  • 1
    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. Jan 9, 2021 at 19:17
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
2
  • tic: Can't open xterm-yellow.ti
    – alper
    Jul 5, 2020 at 0: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 5, 2020 at 8: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.

6
  • 11
    It 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
  • 3
    Please 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
  • The link is dead.
    – farzan
    Jul 5, 2016 at 10:12
  • Found this mostlike.txt online. Sep 7, 2016 at 15:13
  • What does the output look like?
    – Tom Hale
    Oct 5, 2016 at 14:03
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.