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?

up vote 73 down vote accepted

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

  • 7
    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
  • 7
    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 '14 at 18:39
  • 3
    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 4 '16 at 7:11
  • 1
    That's a lot of sub-shells created. What is the advantage over this simpler approach? – Tom Hale Jan 20 '17 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 at 19:07

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
    most should be installed, for this to work. – enzotib Sep 14 '11 at 7:41
  • 1
    most has strange keybindings... which are not configurable. – Mateen Ulhaq Jun 18 at 8:19

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.

  • 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 '17 at 22:58
  • @Ben thanks. Added above. – Tom Hale Jul 30 at 4:55
  • 1
    For the win!! I love the % display export MANPAGER='less -s -M +Gg' adds. – MikeyE Oct 11 at 14:00

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

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 7 at 19:14

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.

  • 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) – Michael Mrozek Aug 10 '10 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. – Stefan Lasiewski Aug 10 '10 at 21:27
  • The link is dead. – farzan Jul 5 '16 at 10:12
  • Found this mostlike.txt online. – Kaushal Modi Sep 7 '16 at 15:13
  • What does the output look like? – Tom Hale Oct 5 '16 at 14:03

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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