2

I'm not an expert in Linux, but I was reading an introductory book and found that man pages use the PAGER environment variable to show the content of man pages. I checked printenv and didn't find any PAGER variable there.

However, since it's a binary, and the path to /usr/bin is in my path, there is a program called pager in Linux as well, and I think it's what is the default option for $PAGER.

Just out of curiosity I would like to know how to modify this PAGER variable. I tried adding one to my bashfile but that didn't work, couldn't find how pager can be changed.

And whats so special about pager - I expect any program that can read from stdin, or support piping stuff, can be pager, can vim be a pager?

Internally it seems the man just uses sed to send data to a defined PAGER variable because doing following works, but fails.

env PAGER=/usr/bin/nano man printf 

and I get an error, like following

enter image description here

4
  • 1
    This might help you stackoverflow.com/questions/16740246/… Commented Oct 13, 2023 at 12:29
  • The issue with a pager is that it needs to accept input piped to it from the process that generates the text, and also take your editor-type commands from the terminal (also on stdin). I don't know the detail of how that is done. However, your problem with nano is that it has no file to edit, but is getting the whole of the man page on stdin and attempting to treat it as editor commands. The junk you see is a consequence of nano changing the terminal modes to suit its use of the ncurses package. Commented Oct 13, 2023 at 12:29
  • It's probably not wise to assume that a command is the default for something just because it's name something. But I have no idea what the 'pager` command on your system does, I don't have one on my system, and don't think I've ever seen one. Commented Oct 14, 2023 at 9:43
  • In general editors (such as vim and nano that you mention are meant to edit things, and man pages are not meant to be edited, so editors are probably bad choices in general. Here you'll additionally run into the problem that processing the man page outputs formatting characters that most editors just display as-is. vim (I don't have nano installed so I can't try that) will also warn that input is not from a terminal, which leads to another defining characteristic, a pager must be able to handle two input streams, one for the data and one for your commands. Commented Oct 14, 2023 at 9:54

2 Answers 2

4

The PAGER variable isn't only used for man, but for any other output that needs to be paged, such as git branch, journalctl, etc.

If you want this setting to be applied only for manpages, you should use MANPAGER instead.

To define vim as the MANPAGER:

export MANPAGER="vim +MANPAGER -" 
  • +MANPAGER puts vim into manpage mode (colors, hide unvisible characters, etc.). This will also make it read-only
  • The - at the end indicates to read text from standard input (stdin)

For other pagers:

  • Nano (won't be pretty as explained by @muru):

    export MANPAGER="nano -" 

For the following you don't need to add the hyphen because these programs are originally desinged to read from stdin.

  • Most:

    export MANPAGER="most" 
  • Less:

    export MANPAGER="less" 
  • More:

    export MANPAGER="more" 
  • Bat (nice colors and formatting):

    export MANPAGER="sh -c 'col -bx | bat -l man -p'" 
1
  • Opps, I didn't know it was $MANPAGER. Commented Oct 13, 2023 at 13:01
1

If you want to use nano as a pager, you need to tell it to read from standard input:

MANPAGER='nano -' man printf 

But the result won't be pretty, since man outputs formatting characters that nano will just present as-is instead of formatting the text.

And yes, this works for Vim too, and Vim has some support for this. See :h manpager.vim for more details.

Internally it seems the man just uses sed to send data

No, man doesn't use sed for anything. It does use troff for formatting the text.


However since it is a binary, and the path to /usr/bin is in my path, there is a program called pager in linux as well, and i think it is what is default option for $PAGER,

If you're using Debian or some Debian derivative, that is the case. From man man:

-P pager, --pager=pager Specify which output pager to use. By default, man uses pager, falling back to cat if pager is not found or is not executable. This option overrides the $MANPAGER environment variable, which in turn overrides the $PAGER environment variable. It is not used in conjunction with -f or -k. 

This is not necessarily the case on other distributions (pager, which is just a symlink to some actual pager, might not even be present).

2
  • what is that sed that is showing in the error picture, My assumption was that sed is being used by the man here. Commented Oct 13, 2023 at 12:57
  • That I can't say. Seems to be some weird interaction between nano and less. Commented Oct 13, 2023 at 13:31

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.