How can I still see the 'man' text after I quit man?
I typically use tcsh or bash and often want to use 'man' to review a command's options. Currently when I quit man or ctrl-C, the man text disappears and I see the scrollback buffer that was there before I performed the 'man' command. I would like to still see the 'man' text I was viewing as a reference while I'm typing the command at the command prompt without opening a second window, how can I do that?
This behavior comes from the “alternate screen” feature included in some (hardware) terminals and most (software) terminal emulators. What happens is that some terminal-aware programs switch to the alternate ‘screen’ to do their work and switch back to the normal ‘screen’ when they are dismissed (quit, suspended, etc.). This effectively clears away the final output of such programs.
A web search for “terminfo prevent altscreen” provided a page that discusses the alternate screen ‘problem’ and several solutions.
As that linked page describes, some programs can be configured to avoid using the alternate screen. The -X
option for less is an indirect way of doing this. Vim can be configured similarly by unsetting the t_ti
and t_te
variables.
A solution with a wider scope is to edit the terminfo entry for your terminal so that it does not include the alternate screen control sequences. The terminfo entries form a database that tell programs which control sequences to send to a terminal to create certain effects (moving the cursor, clearing the screen, erasing part of the current line, etc.). By editing the terminfo entry that your terminal uses, you can arrange to ‘neglect to tell’ all terminfo-using programs that the “alternate screen” control sequences even exist.
From the list of the control sequences for xterm, we can see that the control sequences of interest (for xterm-like terminal emulators) are
- enable alternate screen:
ESC [ ? 47 h
(likewise for 1047 and 1049), and - restore normal screen:
ESC [ ? 47 l
(likewise for 1047 and 1049).
These codes are most likely present in the terminfo variables smcup
and rmcup
. A quick and dirty way to excise these variables for your current TERM might look like this:
infocmp | sed -e 's/[sr]mcup=[^,]*,//' > /tmp/noaltscreen-terminfo
tic -o ~/.terminfo/ /tmp/noaltscreen-terminfo
This method is quite crude and will certainly not work for all terminals, but it will probably work for most xterm-like terminal emulators. Ideally you should investigate the values in the smcup
and rmcup
variables of the terminfo entry for your terminal and conservatively edit them to prevent the undesired behavior. The above-linked discussion/solution page has a “pre-cooked” terminfo file that you might use instead of hacking your own (but what is the fun in that?).
You need to set $MANPAGER
to /usr/bin/less -isX
, replacing the path where less
is found as required. The important part is the -X
; the -is
is the default set passed to less.
Assuming your pager is less
, set the environment variable LESS
to include "X". For example mine is:
LESS="-iMFXRj4a"
You can set this in your ~/.bashrc
file
export LESS="-X"
and include any other options you alrady have or add the ones you like.
Another trick - less elegant than the other answers - is:
man what | more
(At least, this works on MacOS X, which is usually similar enough for it to be valid.)