Skip to content

Instantly share code, notes, and snippets.

@bahamas10
Last active May 5, 2026 20:25
Show Gist options
  • Select an option

  • Save bahamas10/542875bb47990933638d2b7dfaa501bf to your computer and use it in GitHub Desktop.

Select an option

Save bahamas10/542875bb47990933638d2b7dfaa501bf to your computer and use it in GitHub Desktop.
Colorize Manpages on the Terminal
# annotated by dave eddy (@yousuckatprogramming)
# explained - https://youtu.be/D0sG2fj0G4Y
# borrowed heavily from https://grml.org
# Begin blinking text mode
# I just use bold red here since my terminal has blinking disabled
export LESS_TERMCAP_mb=$'\e[1;31m'
# Begin bold text mode
export LESS_TERMCAP_md=$'\e[1;31m'
# End all special formatting started by mb/md/etc.
export LESS_TERMCAP_me=$'\e[0m'
# End standout mode
export LESS_TERMCAP_se=$'\e[0m'
# Begin standout mode
# search results - bold, yellow foreground, blue background.
export LESS_TERMCAP_so=$'\e[1;33;44m'
# End underline mode
export LESS_TERMCAP_ue=$'\e[0m'
# Begin underline mode
# underline and bold green
export LESS_TERMCAP_us=$'\e[4;1;32m'
# Begin reverse-video mode
export LESS_TERMCAP_mr=$'\e[7m'
# Begin dim/half-bright mode
export LESS_TERMCAP_mh=$'\e[2m'
# Begin subscript mode
# (probably isn't supported)
export LESS_TERMCAP_ZN=$'\e[74m'
# End subscript mode
# (probably isn't supported)
export LESS_TERMCAP_ZV=$'\e[75m'
# Begin superscript mode
# (probably isn't supported)
export LESS_TERMCAP_ZO=$'\e[73m'
# End superscript mode
# (probably isn't supported)
export LESS_TERMCAP_ZW=$'\e[75m'
# Finally wire up `man` to use `less`
# this is usually the default but let's just be sure
export MANPAGER='less'
export LESS_TERMCAP_mb=$'\e[1;31m'
export LESS_TERMCAP_md=$'\e[1;31m'
export LESS_TERMCAP_me=$'\e[0m'
export LESS_TERMCAP_se=$'\e[0m'
export LESS_TERMCAP_so=$'\e[1;33;44m'
export LESS_TERMCAP_ue=$'\e[0m'
export LESS_TERMCAP_us=$'\e[4;1;32m'
export LESS_TERMCAP_mr=$'\e[7m'
export LESS_TERMCAP_mh=$'\e[2m'
export LESS_TERMCAP_ZN=$'\e[74m'
export LESS_TERMCAP_ZV=$'\e[75m'
export LESS_TERMCAP_ZO=$'\e[73m'
export LESS_TERMCAP_ZW=$'\e[75m'
export MANPAGER='less'
export LESS_TERMCAP_mb=$(tput bold; tput setaf 1)
export LESS_TERMCAP_md=$(tput bold; tput setaf 1)
export LESS_TERMCAP_me=$(tput sgr0)
export LESS_TERMCAP_se=$(tput sgr0)
export LESS_TERMCAP_so=$(tput bold; tput setaf 3; tput setab 4)
export LESS_TERMCAP_ue=$(tput sgr0)
export LESS_TERMCAP_us=$(tput smul; tput bold; tput setaf 2)
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 MANPAGER='less'
@eskay993
Copy link
Copy Markdown

Works great on zsh! Except it messes up printenv and starts colouring as soon as the LESS_TERMCAP vars are displayed and goes all weird. There are likely other scenarios too. Only workaround I can think of is to pipe printenv to something that clears the colors (like cat -v but that seems hacky.

Cool tips but will stick to bat colouring man pages, unless someone knows a fix?

@brtwrst
Copy link
Copy Markdown

brtwrst commented Apr 28, 2026

Works great on zsh! Except it messes up printenv and starts colouring as soon as the LESS_TERMCAP vars are displayed and goes all weird. There are likely other scenarios too. Only workaround I can think of is to pipe printenv to something that clears the colors (like cat -v but that seems hacky.

Cool tips but will stick to bat colouring man pages, unless someone knows a fix?

I fixed that by just creating the following bash function in my .bashrc

man() {
    LESS_TERMCAP_mb=$(tput bold; tput setaf 1) \
    LESS_TERMCAP_md=$(tput bold; tput setaf 1) \
    LESS_TERMCAP_me=$(tput sgr0) \
    LESS_TERMCAP_se=$(tput sgr0) \
    LESS_TERMCAP_so=$(tput bold; tput setaf 3; tput setab 4) \
    LESS_TERMCAP_ue=$(tput sgr0) \
    LESS_TERMCAP_us=$(tput smul; tput bold; tput setaf 2) \
    LESS_TERMCAP_mr=$(tput rev) \
    LESS_TERMCAP_mh=$(tput dim) \
    LESS_TERMCAP_ZN=$(tput ssubm) \
    LESS_TERMCAP_ZV=$(tput rsubm) \
    LESS_TERMCAP_ZO=$(tput ssupm) \
    LESS_TERMCAP_ZW=$(tput rsupm) \
    GROFF_NO_SGR=1 \
    command man "$@"
}

That way they don't pollute your environment.

@eskay993
Copy link
Copy Markdown

That way they don't pollute your environment.

Thank you! Worked a treat. It colors better than bat (which has some formatting issues), so happily using this method now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment