Search code examples
bashshellcolorsfishmanpage

How to colorize man-page in Fish shell?


The bash solution described tmux man-page search highlighting works great in bash but fail when ported in fish.

Fish config

function configure_pager
    # Colored man pages: http://linuxtidbits.wordpress.com/2009/03/23/less-colors-for-man-pages/
    # Less Colors for Man Pages
    set -gx LESS_TERMCAP_mb '\E[01;31m'       # begin blinking
    set -gx LESS_TERMCAP_md '\E[01;38;5;74m'  # begin bold
    set -gx LESS_TERMCAP_me '\E[0m'           # end mode
    set -gx LESS_TERMCAP_se '\E[0m'           # end standout-mode
    set -gx LESS_TERMCAP_so '\E[38;5;016m\E[48;5;220m'    # begin standout-mode - info box
    set -gx LESS_TERMCAP_ue '\E[0m'           # end underline
    set -gx LESS_TERMCAP_us '\E[04;38;5;146m' # begin underline
end

Rendering in fish

Instead of the color I got the un-interpreted code: LS(1) User Commands LS(1)

\E[01;38;5;74mNAME\E[0m
       ls - list directory contents

\E[01;38;5;74mSYNOPSIS\E[0m
       \E[01;38;5;74mls\E[0m [\E[04;38;5;146mOPTION\E[0m]... [\E[04;38;5;146mFILE\E[0m]...

\E[01;38;5;74mDESCRIPTION\E[0m
       List information about the FILEs (the current directory by default).  Sort entries alphabetically if none of \E[01;38;5;74m-cftuvSUX\E[0m nor \E[01;38;5;74m--sort\E[0m is specified.

Question

This might be due to me using a wrong syntax for string literal variable in fish:

set -gx LESS_TERMCAP_mb '\E[01;31m'       # begin blinking

original bash is:

export LESS_TERMCAP_mb=$'\E[01;31m'       # begin blinking

What is the correct syntax for color coding in fish?


Solution

  • What you are doing in the bash version is ANSI-C Quoting. This makes bash interpret the sequence before setting it to the variable. So LESS_TERMCAP_mb doesn't contain the literal string "\E[01;31m", but the sequence specified by that - in particular, the "\E" is the escape character.

    In fish, what you want to do is specify the escape sequence outside of quotes - see the section on quotes in the fish documentation:

    set -gx LESS_TERMCAP_mb \e'[01;31m'
    

    and so on.

    Preview

    fish man page highlight configuration