How does 'man' actually display the pages?

When you normally execute, say man 3 printf, man simply does the job of looking up and fetching the particular manpage. The actual act of displaying it, is done by a pager.

The default pager that man uses, less, is in essence a simple TUI program that allows for easy navigation of a particular text buffer (scrolling, searching, etc… all using vi-like keybindings). It can output stylized text (bolds, underlines, etc…) which it does by default for manpages.

If less is not found on the system, man falls back to pure cat.

A specific pager can be chosen by setting the --pager option, or by using the MANPAGER/PAGER environment variables (the former of which has priority).

Colorized manpages

Colorization in manpages is possible when using less by setting LESS_TERMCAP_* environment variables (see here for info and a bit of history).

Those strings represent ANSI escape sequences that less outputs in certain situations. For example, LESS_TERMCAP_md is the sequence that less will output whenever it’s instructed to bold a specific part of the text.

The strings have quite cryptic names as they’re artifacts of an old library (termcap) that less used to access the terminal back in the day, but that were kept for compatibility reasons. These variables also aren’t mentioned anywhere in less’ documentation.

Below is a fish function that I use to achieve colorization in manpages. It exports a set of these LESS_TERMCAP_* variables to my preference, and then simply runs man, which in turn runs less:

 1function man --description "manpages using the less pager but with 8bit colors"
 2    set -x LESS_TERMCAP_md (echo -e '\e[01;31m')
 3    set -x LESS_TERMCAP_me (echo -e '\e[0m')
 4    set -x LESS_TERMCAP_se (echo -e '\e[0m')
 5    set -x LESS_TERMCAP_so (echo -e '\e[01;44;33m')
 6    set -x LESS_TERMCAP_ue (echo -e '\e[0m') 
 7    set -x LESS_TERMCAP_us (echo -e '\e[01;32m')
 8
 9    command man $argv
10end