Thursday, May 8, 2008

How to prevent Linux man pages from clearing after you quit reading

Man pages are excellent resources for learning the specifics of a Linux command. After all, who can remember all the nitty gritty of a command?

One annoyance of reading man pages on some Linux distributions is that after you quit reading it, the contents are cleared off screen. The man page contents simply don't stay around after you quit man. If that happens to you, it means that the default pager for viewing man pages is the less command, and that is how less behaves.

Wiping man contents out or not is a personal preference. Some may like the man stuff being wiped out because it won't clutter up the command window. However, there are times when you want the man contents to be visible after you finish reading it. You will have that information in front of you when you enter the next command.

The good news is that you can change the man page behavior. This is done by changing the default pager from less to something like more.

[beranger-org has a great/better suggestion: instead of changing to more, use less -X]
You can change it permanently or on demand, and for everyone or just individual users. Because it is a personal preference, I would recommend changing it only for yourself.

First, you need to find out where more is.
$ which more
/bin/more


Add the following line in the .bashrc file in your home directory:
export PAGER=/bin/more 


If you want to stick with less, add this line instead.
export PAGER='less -X' 


That customizes the PAGER environment variable every time a shell process is started.

Beware that the default pager is also used in commands other than man. less is a more powerful pager than more. You may wish just to change the PAGER for the current shell session. This can be done by typing in the exact export statement above into the command line.

If you want to revert to the default PAGER (i.e., less), enter this:
$ unset PAGER


If you really insist on changing the default man behavior permanently for everyone on your system, edit the file /etc/man.config (on RedHat-based systems) or /etc/manpath.config on Debian-based systems, and change the line for PAGER. This affects only man page viewing. Alternatively, you can run update-alternatives --config pager (for Debian) or alternatives --config pager (for RedHat) to globally change the PAGER environment variable for all applications.



This is how you can control man when it exits.

14 comments:

Anonymous said...

export PAGER='/usr/bin/less -X'

or

export PAGER='/usr/bin/less --no-init'

is a better choice for me.

Peter Leung said...

You are absolutely right. less -X is definitely better than more. I'll add your suggestion to the body of the blog entry.

Thanks

Anonymous said...

Nice little trick there. Can you think of a way in which the more behavior, or the -X behavior for less could be selectively applied to the man command, therefore excluding the change from other commands that use less?

Peter Leung said...

"edit the file /etc/man.config (on RedHat-based systems) or /etc/manpath.config on Debian-based systems, and change the line for PAGER"

On my Debian Etch system,
/etc/manpath.config has a line

#DEFINE pager /usr/bin/pager -s

Uncomment it and make it look like this:
DEFINE pager /usr/bin/less -X

Anonymous said...

For just the instance
$ PAGER='/usr/bin/less -X' man printf

Anonymous said...

I prefer most as my pager. Among other things, it puts manpage keywords and etc in color, red or green here. Of course, it won't be a lot of help to the colorblind or those still using monochrome screens, but it's sure useful otherwise, making manpages much more useful as the lookup references they are.

However, unlike less and more, most is often a non-default separate package, so you'll likely have to specifically install it.

There's another possibility as well, for those reading manpages in a terminal window or other environment with a good scrollback buffer. In that case, a pager can be more irritating than not, since it kills the ability to simply use the scroll buffer as the pager. Setting PAGER=cat solves that problem -- anything that would normally be paged is simply output unpaged, allowing one to use the terminal [window] built-in scroller, where available. Of course, this doesn't work so well in a real terminal without scrollback, or in a VT with a limited scrollback buffer that gets cleared when switching between VTs.

--
Duncan
"Every nonfree program has a lord, a master --
and if you use the program, he is your master." Richard Stallman

Anonymous said...

Thanks for the config file trick Mr. Leung. I think that will be quite useful in the future.

Anonymous said...

As I agree with anonymous@May 10, 2008 3:58 AM ("I prefer most as my pager. Among other things, it puts manpage keywords and etc in color"), I have another suggestion for using less -X just for the current manpage. Just pipe the manpage to less:

man most | less -X

That said, if you read a manpage and suddenly realize you wish to read it with less -X again, it just involves quitting, getting the last command and appending the pipe.

Thanks for the article, anyway, that's just what I've been missing for quite some while (I use egrepping manpages through pipes a lot, thus less -X is a great addition for me).

Peter Leung said...

Those were all good ideas. It gets me thinking ....

How about making it an alias and putting it in your .bashrc file?

alias man='PAGER=most man'

OR if you like
alias man='PAGER='\''less -X'\'' man'


It will only affect you, not other users, and only for man pages.

Anonymous said...

Peter,

indeed I think now there's a solution for everybody's needs:

> alias man='PAGER='\''less -X'\'' man'

I didn't know that you can change environment variables this way. Great. So, you can also define two aliases for man, one with the default behaviour, one with no clearing (e. g., manx for clearing and man for default behaviour, or vice versa).

I'm the anonymous from May 10, 2008 1:36 PM and, in most cases, I prefer the way man actually works. While typing commands, I usually need to have a quick glance at a manpage, but after quitting I like to see all the previous commands I've already typed. Therefore, I personally will stick with my proposition, since I can use it on any computer as any user, no matter what the environment variables are. And it's simple to remember, since I use less all the time ...

Andrew said...

would just like to add here that if changing your ~/.bashrc file does not seem to make a difference, try editing ~/.bash_profile instead. that did the trick for me.

alias less='/usr/bin/less -X'

Anonymous said...

Or, once you are reading the page and decide you want it to stick around, there is the simple;
-!X
command - this is given while in the pager (while reading a man page) and doesn't need to be configured for anybody.

Anonymous said...

Also, if you alias the man page to use less -X, you can preceed the man command with a backslash if you want to use the original form of the command...

Justin said...

Thanks for the config file trick Mr. Leung. I think that will be quite useful in the future....