Skip to content

Instantly share code, notes, and snippets.

@supermarin
Last active April 2, 2024 13:57
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save supermarin/6dca255da372c3f9eb26 to your computer and use it in GitHub Desktop.
Save supermarin/6dca255da372c3f9eb26 to your computer and use it in GitHub Desktop.
Colored `man` pages on OSX
# ZSH / BASH users
# Add this to your .env, .bashrc, .zshrc, or whatever file you're using for environment
man() {
env \
LESS_TERMCAP_mb=$(printf "\e[1;31m") \
LESS_TERMCAP_md=$(printf "\e[1;31m") \
LESS_TERMCAP_me=$(printf "\e[0m") \
LESS_TERMCAP_se=$(printf "\e[0m") \
LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
LESS_TERMCAP_ue=$(printf "\e[0m") \
LESS_TERMCAP_us=$(printf "\e[1;32m") \
man "$@"
}
# Fish users
# Save this file as ~/.config/fish/functions/man.fish
function man
env \
LESS_TERMCAP_mb=(printf "\e[1;31m") \
LESS_TERMCAP_md=(printf "\e[1;31m") \
LESS_TERMCAP_me=(printf "\e[0m") \
LESS_TERMCAP_se=(printf "\e[0m") \
LESS_TERMCAP_so=(printf "\e[1;44;33m") \
LESS_TERMCAP_ue=(printf "\e[0m") \
LESS_TERMCAP_us=(printf "\e[1;32m") \
man "$argv"
end
@cafedomingo
Copy link

๐Ÿ‘

@gf3
Copy link

gf3 commented Aug 29, 2016

you'll need to remove the quotes around "$@" and "$argv", otherwise it passes multiple arguments as a single argument; which makes it impossible to view different sections of the manual. e.g.: man 3 calloc

@suewonjp
Copy link

๐Ÿ‘

BTW, @gf3 is telling incorrect info.

"$@" is supposed to pass multiple arguments. "$*" will pass multiple arguments as a single argument, though.
(Therefore, .env file is correct, at least)

See https://yakking.branchable.com/posts/whitespace-safety/

@sankalp-khare
Copy link

@supermarin, thanks for that man.fish! ๐Ÿ™‡โ€โ™‚๏ธ
Also what @gf3 says is correct, at least for fish --- removing the quotes from around "$argv" fixes the issue with invocations such as man 3 calloc. Thanks @gf3 for pointing it out ๐Ÿ‘

@mor4thii
Copy link

For Oh-my-fish users, you can also put the man function into your init.fish file, for example.
That way, you can keep your omf config in a repository and have colored man output.

Besides, I can confirm what @sankalp-khare said regarding $argv in fish. Fish does not perform word splitting and argv is an array (or, list in fish terminology).

@matrael
Copy link

matrael commented Nov 27, 2023

For anyone using a version of less that is more recent than 2021, you can make use of the new '--use-color' option like so:

export MANPAGER="less -R --use-color -Dd+r -Du+b"

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