11

I have a problem using these methods to get colors in my man pages in gentoo. I've asked already in the gentoo forums but it still doesn't work, and the comments in a bug report (Bug 184604) don't work either. Can someone help me to get colours in my man pages?

EDIT: Yes, I'm using less as pager and urxvt-unicode as terminal emulator

EDIT2:

I already asked in the gentoo forums but it didn't help, this is the link: http://forums.gentoo.org/viewtopic-t-819833-start-0.html.

Vicfred
  • 284

3 Answers3

26

Could be a number of problems.

Seeing as you're using zsh, try putting this in your ~/.zshrc:

export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;31m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;47;34m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;32m'
export LESS=-r

Then open a new terminal window and try running

man ls

if it's not working, run each of the following to find out where the problem is:


Number 1

typeset -p LESS_TERMCAP_md | cat -v

should print

typeset -x LESS_TERMCAP_md="^[[01;31m"

and

typeset -p LESS

should print

typeset -x LESS="-r"

if not, you put the export LESS stuff in the wrong file.


Number 2

echo "${LESS_TERMCAP_md}red${LESS_TERMCAP_me}"

should print

red

in a red color. If it doesn't there is something wrong with your terminal settings. Check your terminal settings (e.g. ~/.Xresources) or try running gnome-terminal or xterm and see if that works.


Number 3

echo -E "a^Ha" | LESS= less -r

(^H must be entered by pressing Ctrl+V then Ctrl+H)

should print

a

in red. If it doesn't, please run these

type less
less --version

and paste the output back in your question.


Number 4

bzcat /usr/share/man/man1/ls.1.bz2 | \
    /bin/sh /usr/bin/nroff -mandoc -Tutf8  | head -n 5 | cat -v

should print

LS(1)                            User Commands                           LS(1)



N^HNA^HAM^HME^HE

(note the ^H like in step number 3)

if it's printing something like:

LS(1)                            User Commands                           LS(1)



^[[1mNAME^[[0m

instead, you will need to find a way to disable "sgr escape sequences". The easiest thing to try is adding export GROFF_NO_SGR=1 to .zshrc, but there are other ways of fixing this.


Number 5

bzcat /usr/share/man/man1/ls.1.bz2 | \
    /bin/sh /usr/bin/nroff -mandoc -Tutf8 | less

should display the ls man page with colors.


man ls

should now be working!

Mikel
  • 57,299
  • 15
  • 134
  • 153
  • I'm using zsh, I couln't post this before because I was a new user http://forums.gentoo.org/viewtopic-t-819833-start-0.html also typeset -p LESS printed this: "typeset: no such variable: LESS" and about the number 5, there's no ls.1.gz but there's ls.1.bz2 but it prints this "gzip: /usr/share/man/man1/ls.1.bz2: not in gzip format" the rest of the options work well but I still don't have coloured man pages. – Vicfred Jan 25 '11 at 05:08
  • typeset -p LESS not working is your problem. But seeing as you are using zsh, you should put export LESS=-r in your ~/.zshrc, not in ~/.bashrc. Please do that then try these steps again. – Mikel Jan 25 '11 at 05:13
  • And change zcat /usr/share/man/man1/ls.1.gz to bzcat /usr/share/man/man1/ls1.bs2. – Mikel Jan 25 '11 at 05:26
  • I have updated the instructions based on the information you just provided. – Mikel Jan 25 '11 at 05:34
  • I added export LESS=-r but number 5 prints a normal man page with no colours. – Vicfred Jan 25 '11 at 05:42
  • 1
    I tried the new options and all of them work except the last one, it prints the man pages with no colours. – Vicfred Jan 25 '11 at 05:49
  • Really? Even in a new terminal? What does typeset -p LESS print in that window? But does man ls give colors? That's obviously the main thing. – Mikel Jan 25 '11 at 05:51
  • typeset -p LESS returns typeset -x LESS=-r but man ls doesn't give colours. – Vicfred Jan 25 '11 at 06:03
  • I have added a new number 4: bzcat... | head -n 5 | cat -v – Mikel Jan 25 '11 at 06:12
  • it prints LS(1) User Commands LS(1)

    ^[[1mNAME^[[0m

    – Vicfred Jan 25 '11 at 06:15
  • We have found your problem! – Mikel Jan 25 '11 at 06:16
  • First thing to try: change /bin/sh /usr/bin/nroff -mandoc -Tutf8 to /bin/sh /usr/bin/nroff -mandoc -Tutf8 -c above. – Mikel Jan 25 '11 at 06:19
  • now it returns LS(1) User Commands LS(1)

    N^HNA^HAM^HME^HE

    – Vicfred Jan 25 '11 at 06:22
  • Cool. There's many things the problem could be, but my best guess: try putting export GROFF_NO_SGR=1 in your .zshrc, open a new terminal, and try man ls again. – Mikel Jan 25 '11 at 06:30
  • If this doesn't work, I will need you to do some reading, and to think if you changed any USE variable or environment variable (e.g. an export line in your .zshrc or other config file) related to color or sgr or man or nroff or groff or grotty. – Mikel Jan 25 '11 at 06:32
  • PS: When it prints N^HNA^HAM^HME^HE, you should be able to pipe that to less by adding | less at the end, and you should see the colors. So we are aiming to have nroff produce this N^HN... output by default. – Mikel Jan 25 '11 at 06:34
  • GROFF_TYPESETTER, GROFF_NO_SGR, LESSCHARSET, LANG, LC_ALL, LC_CTYPE, man.conf, and --enable-sgr are the the main things we need to look for. – Mikel Jan 25 '11 at 06:36
  • 8
    export GROFF_NO_SGR=1 worked, thank you, could you point me to some references on this? you were the only one who actually gave a different answer. – Vicfred Jan 25 '11 at 06:37
  • Most of it's based on things I have learned over many years, e.g. man runs nroff; old systems would make letters bold by printing the letter, backspacing, then printing the letter again, like a printer that uses ink would (printing the letter twice makes the letter darker); information about LESS_TERMCAP_md is under md in man 5 terminfo, and searching for "xterm color escape sequences". For the last part, my references were the man pages for less, nroff, groff, and grotty. And of course, zsh commands to run for every zsh shell should go in .zshrc. – Mikel Jan 25 '11 at 06:43
  • There is also tput(1), perhaps this command provides an alternative way to change the terminal's behaviour, but I couldn't get this command to do anything. Perhaps someone more knowledegable can provide more clues. But the env var works for me, thanks! – drumfire Mar 16 '13 at 18:18
  • @mikel Thanks a lot for GROFF_NO_SGR=1; it fixed grotty 1.23 breaking man page colours after a recent (2023 Jul) update. It used to work up till 1.22.4 (2023 Mar) without the env variable. – legends2k Jul 09 '23 at 12:12
4

Having read Mikels answer, its very comprehensive, and will hopefully work on all systems, but its probably overkill for myself ( also on a gentoo system. )

So here is a result for people who are very "Do long, didn't read" with all that shenanigans.

The Bare minimum I could do to get Coloured Man pages is as follows:

LESS_TERMCAP_md=$'\E[01;31m' LESS_TERMCAP_me=$'\E[0m' GROFF_NO_SGR=1 man man

This renders text that was previously only in bold, in bold and red. ( The red characterised by the '31' in LESS_TERMCAP_md )

LESS_TERMCAP_se=$'\E[0m' LESS_TERMCAP_so=$'\E[01;32m'

These 2 rules control the formatting of the current line number display, and coerce it to rendering in Green.

LESS_TERMCAP_us=$'\E[04;33m' LESS_TERMCAP_ue=$'\E[0m'

These 2 rules move everything that was previously underlined, to being underlined and yellow. ( 04 is 'underlined', 33 is "yellow" )

0

People searching for solutions to this in 2023 might be affected by a change in the groff package introduced in version 1.23.0.

Putting export MANROFFOPT=-c in my .zshrc fixed it for me.

(or alternatively GROFF_NO_SGR=1)

Background:

https://bbs.archlinux.org/viewtopic.php?id=287185

https://github.com/jedsoft/most/issues/18

user4514
  • 101