Skip to content

Freed-Wu/manpager

Repository files navigation

manpager

pre-commit.ci status github/workflow codecov DeepSource

github/downloads github/downloads/latest github/issues github/issues-closed github/issues-pr github/issues-pr-closed github/discussions github/milestones github/forks github/stars github/watchers github/contributors github/commit-activity github/last-commit github/release-date

github/license github/languages github/languages/top github/directory-file-count github/code-size github/repo-size github/v

Colorize man XXX.

man

Dependencies

This plugin will do nothing without dependencies.

Install

deb

Download *.deb firstly.

deb -i *.deb

rpm

Download *.rpm firstly.

rpm -i *.rpm
yay -S sh-manpager
nix-env -iA nur.repos.Freed-Wu.manpager

homebrew

brew tap Freed-Wu/manpager https://github.com/Freed-Wu/manpager
brew install manpager

Configure

Add the following code to your .bash_profile, .zprofile, ...

eval "$(manpager)"

Customize

You can define $MANPAGER to customize the colorization of man XXX.

Such as:

Disable Paging

export MANPAGER='manpager --paging=never'

Define n and p to Jump Forward and Backward Between Sections

export MANPAGER='manpager | less --pattern=^\\S+'

That is because man XXX looks like

MAN(1)                        Manual pager utils                        MAN(1)

NAME
       man - an interface to the system reference manuals

SYNOPSIS
       man [man options] [[section] page ...] ...
       ...

DESCRIPTION
       man is the system's manual pager.  Each page argument given to man is
       ...

So we can search ^\S+ forward and backward to jump between the lines started with NAME, SYNOPSIS, DESCRIPTION, etc.

Force to Colorize man XXX

You can man XXX | manpager to force to colorize man XXX.

Similar Projects

  • zsh-help: colorize XXX --help.
  • lesspipe: colorize less XXX by export LESSOPEN='|lesspipe.sh %s'.
  • bat-extras: provide a program named batman and user can batman XXX to get colorized man XXX. Right, you can alias man=batman to colorize man XXX. However:
    • Not every shell has aliases, such as !man XXX on ptpython/ipython
    • man has shell completions, batman doesn't have.

Details under the Hood

There are two different realizations of man:

So, our manpager must be compatible for both of them. There are some differences between them:

  • For export MANPAGER="sh -c 'col -bx | bat -plman'":
    • man-db will split it to get ["sh", "-c", "col -bx | bat -plman"].
    • mandoc will split it to get ["sh", "-c", "'col", "-bx", "|", "bat", "-plman"]. How terrible!
  • For MANPAGER='manpager --option' man foobar:
    • man-db will execute MANPAGER='' man foobar | manpager --option. Yes, there exists a pipe.
    • mandoc will generate a temp file /tmp/man.XXXX?????? which ? is a random alphabet as the output of man foobar, then execute manpager --option /tmp/man.XXXX??????

So we use [ -t 0 ] to judge current man is which man, then wrap it.