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?).
strace man
reveals that it just calls pager
.
man pager
reveals that pager
is actually less
. And indeed
$ ls -al /usr/bin/pager
lrwxrwxrwx 1 root root 23 2009-08-18 17:30 /usr/bin/pager -> /etc/alternatives/pager
lrwxrwxrwx 1 root root 13 2009-08-18 17:19 /etc/alternatives/pager -> /usr/bin/less
so less has some presets which are invoked when called by the name pager
.
Actually strace also reveals these environment variables:
"LESSCHARSET=utf-8",
"LESS=-ix8RmPm Manual page less(1) ?ltline %lt?L/%L.:byte %bB?s/%s..?e
(END):?pB %pB\\%..$PM Manual page less(1) ?ltline %lt?L/%L.:byte %bB?s/%s..?e
(END):?pB %pB\\%..$",
So that's it!
Best Answer
You can access the command line using bang (
!
) within less.So for example, if you type:
And temp.txt should be gone.
Edit: By default it seems that man now uses less to page (for some reason I thought it used
more
, maybe in the past it did). You can use the same trick, but it requires the full path (eg. /home/user/...) to get it to work.This is because invoking man changes the current working directory. On my machine (xubuntu, using
xfce-terminal
) it goes to/usr/share/man
. If your console displays the CWD you can see it change, or you can see it from withinman
by entering: