Colors in Man Pages

39,954

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

    ss of man page

Solution 2

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.

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)

  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.

Share:
39,954

Related videos on Youtube

xenoterracide
Author by

xenoterracide

Former Linux System Administrator, now full time Java Software Engineer.

Updated on September 17, 2022

Comments

  • xenoterracide
    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 my xterm's (e.g. konsole) is there any way I can enable this? hopefully a fairly simple solution?

  • Michael Mrozek
    Michael Mrozek over 13 years
    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)
  • Mark Norgren
    Mark Norgren over 13 years
    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.
  • Kent Fredric
    Kent Fredric about 13 years
    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.
  • enzotib
    enzotib over 12 years
    most should be installed, for this to work.
  • celtschk
    celtschk about 10 years
    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.
  • CMCDragonkai
    CMCDragonkai about 8 years
    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.
  • Mark Norgren
    Mark Norgren about 8 years
    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.
  • farzan
    farzan almost 8 years
    The link is dead.
  • Kaushal Modi
    Kaushal Modi over 7 years
    Found this mostlike.txt online.
  • Kaushal Modi
    Kaushal Modi over 7 years
    Works for me on tmux in uxterm. Thanks!
  • Tom Hale
    Tom Hale over 7 years
    What does the output look like?
  • Marius
    Marius over 7 years
    It's intended to show bold as bright-red, and underline as green. But I wouldn't use it as written.
  • Tom Hale
    Tom Hale over 7 years
    That's a lot of sub-shells created. What is the advantage over this simpler approach?
  • Ben
    Ben over 7 years
    As @CMCDragonkai mentioned in a comment on another answer, this requires export GROFF_NO_SGR=1 to work on some terminal emulators.
  • oHo
    oHo almost 7 years
    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
  • Mateen Ulhaq
    Mateen Ulhaq almost 6 years
    most has strange keybindings... which are not configurable.
  • Mark G.
    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
    Will over 5 years
    to avoid opening nano, you can append to bashrc from the terminal: sudo apt install most; echo "export PAGER='most'" >> ~/.bashrc; source ~/.bashrc
  • MikeyE
    MikeyE over 5 years
    For the win!! I love the % display export MANPAGER='less -s -M +Gg' adds.
  • Terry Wang
    Terry Wang almost 4 years
    This one works better than the Arch Linux Wiki implementation, reusable, nice. Thanks!
  • Terry Wang
    Terry Wang almost 4 years
    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 ;-)
  • alper
    alper almost 4 years
    using colored-man-pages freezes my man page during search
  • tremby
    tremby almost 4 years
    You should tell its developers about that.
  • alper
    alper almost 4 years
    tic: Can't open xterm-yellow.ti
  • alper
    alper almost 4 years
    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
    alper almost 4 years
    During search this does not highlights the selected work in different color, is it possible to have?
  • Shane Bishop
    Shane Bishop over 3 years
    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.
  • Admin
    Admin almost 2 years
    Regarding ZN and ssubm, etc: less doesn't use ZN, and the other subscript-related capabilities (which are defined in the mintty description).
  • Admin
    Admin almost 2 years
    And less doesn't actually use termcap on any system you're likely to be using. Just the termcap interface to terminfo.