12

There seems to be a way to get man pages to display in color (see here. It involves setting environment variables associated with less and adding these definitions to .bashrc. I tried doing the same in config.fish for the fish shell, but got no color output.

How to get color man pages in the fish shell?

5 Answers5

8

If you want these colors to be added only when viewing man pages, not for everything you view in less, you should set these variables in a wrapper function for man instead of putting them in your config.fish.

The whole process is to create a new file at ~/.config/fish/functions/man.fish, and inside it define a function man that sets the necessary environment variables, then calls the original man using command, passing in arguments using $argv.

This is my version of the wrapper function:

~/.config/fish/functions/man.fish

function man --description "wrap the 'man' manual page opener to use color in formatting"
  # based on this group of settings and explanation for them:
  # http://boredzo.org/blog/archives/2016-08-15/colorized-man-pages-understood-and-customized
  # converted to Fish shell syntax thanks to this page:
  # http://askubuntu.com/questions/522599/how-to-get-color-man-pages-under-fish-shell/650192

  # start of bold:
  set -x LESS_TERMCAP_md (set_color --bold red)
  # end of all formatting:
  set -x LESS_TERMCAP_me (set_color normal)

  # start of standout (inverted colors):
  #set -x LESS_TERMCAP_so (set_color --reverse)
  # end of standout (inverted colors):
  #set -x LESS_TERMCAP_se (set_color normal)
  # (no change – I like the default)

  # start of underline:
  #set -x LESS_TERMCAP_us (set_color --underline)
  # end of underline:
  #set -x LESS_TERMCAP_ue (set_color normal)
  # (no change – I like the default)

  command man $argv
end
Rory O'Kane
  • 335
  • 3
  • 11
7

You can set the configuration by the following commands,

set -x LESS_TERMCAP_mb (printf "\033[01;31m")  
set -x LESS_TERMCAP_md (printf "\033[01;31m")  
set -x LESS_TERMCAP_me (printf "\033[0m")  
set -x LESS_TERMCAP_se (printf "\033[0m")  
set -x LESS_TERMCAP_so (printf "\033[01;44;33m")  
set -x LESS_TERMCAP_ue (printf "\033[0m")  
set -x LESS_TERMCAP_us (printf "\033[01;32m")  
sugab
  • 4,417
3

Assuming you use less as your pager, put this in ~/.config/fish/config.fish:

set -x LESS_TERMCAP_mb (printf "\e[01;31m")
set -x LESS_TERMCAP_md (printf "\e[01;31m")
set -x LESS_TERMCAP_me (printf "\e[0m")
set -x LESS_TERMCAP_se (printf "\e[0m")
set -x LESS_TERMCAP_so (printf "\e[01;44;33m")
set -x LESS_TERMCAP_ue (printf "\e[0m")
set -x LESS_TERMCAP_us (printf "\e[01;32m")

If you see \e[0m etc appearing when you view the man page, try adding this line as well:

set -x LESS "-R"
muru
  • 207,228
bobbaluba
  • 345
2

It's possible to use set_color instead of direct ANSI sequences. In fact, this allows you to use any color you want, by using 24-bit color hexadecimal escapes, like (set_color FF55AA).

set -x LESS_TERMCAP_mb (set_color brred)
set -x LESS_TERMCAP_md (set_color brred)
set -x LESS_TERMCAP_me (set_color normal)
set -x LESS_TERMCAP_se (set_color normal)
set -x LESS_TERMCAP_so (set_color -b blue bryellow)
set -x LESS_TERMCAP_ue (set_color normal)
set -x LESS_TERMCAP_us (set_color brgreen)
null
  • 528
0

I suggest a very nice tool called batcat provided by https://github.com/sharkdp/bat.

bat package is available on Ubuntu since 20.04 ("Focal") and Debian since August 2021 (Debian 11 - "Bullseye")and its command is batcat which is a cat replacement with wings.

From the git docu:

man

bat can be used as a colorizing pager for man, by setting the MANPAGER environment variable:

bash export MANPAGER="sh -c 'col -bx | batcat -l man -p'"

It might also be necessary to set MANROFFOPT="-c" if you experience formatting problems.

If you prefer to have this bundled in a new command, you can also use batman.

Note that the Manpage syntax is developed in this repository and still needs some work.

Also, note that this will not work with Mandocs man implementation.

Install this bat package

sudo apt install bat

I love the Monokai theme and therefore I added this command into my .zshrc:

export MANPAGER="sh -c 'col -bx | batcat -l man -p --theme 'Monokai Extended''"

In case you are using fish shell add this line to your ~/.config/fish/config.fish file:

set -x MANPAGER "sh -c 'col -bx | batcat -l man -p --theme 'Monokai Extended''"

A list of installed color-themes can be printed by following command

batcat --list-themes

Here a screenshot of man 3 printf

enter image description here

abu_bua
  • 11,313