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?

9

5 Answers 5

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
1
  • 4
    Awesome thanks! And to get even more fishy you can use the set_color command. e.g.: set -x LESS_TERMCAP_md (set_color -o red), set -x LESS_TERMCAP_me (set_color normal) Apr 13, 2017 at 12:36
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")  
0
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"
3
  • Not working. I see the \e[01 strings when I do "man ls". Oct 28, 2014 at 14:44
  • Strange, I believe this was straight from my config file. I haven't actually tried it on Ubuntu, though, as I run a different distro myself. Maybe you could try adding set -x LESS="-R" to the end of your config?
    – bobbaluba
    Oct 29, 2014 at 8:07
  • Adding that did not help. Still seeing same \e[01 strings in manpage. Oct 29, 2014 at 9:38
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)
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

1
  • You may need to add export MANROFFOPT="-c" to make this work correctly
    – Flimm
    Mar 6 at 14:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .