The feature seems to be called 'alternate screen' or switching between normal and alternate screen.
You can explore it using an xterm. For example type
man man in an
xterm and exit man. Now you can switch to the alternate screen via Ctrl+Mouse2 (middle click) -> 'Show alternate screen'. Alternatively you can directly enter the xterm control sequences, e.g.:
$ echo -e '\033[?47h' # alternate screen
$ echo -e '\033[?47l' # normal screen
The last two commands also work in gnome-terminal (probably in others, too).
What happens when
less etc. startup is basically they send an abstract clear-screen command. On exit (or suspend) they send an abstract restore-screen command. How the command translates to concrete terminal control sequences is defined in the terminfo database.
Disable alternate screen switching
What should work for all terminals is to adjust local terminfo entries, i.e. removing the mapping of abstract clear/restore screen commands.
You can do it like this - for the current
$TERM entry (for gnome-terminal on Fedora 19 for example):
$ echo $TERM
$ infocmp -1 > xterm-256color
$ sed 's/^\(xterm-256color\)|/\1-na|/ ; /smcup\|rmcup/d ' \
xterm-256color > xterm-256color-na
$ diff -u xterm-256color*
--- xterm-256color 2013-08-04 16:33:52.041393461 +0200
+++ xterm-256color-na 2013-08-04 16:36:56.829930520 +0200
@@ -1,5 +1,5 @@
# Reconstructed via infocmp from file: /usr/share/terminfo/x/xterm-256color
-xterm-256color|xterm with 256 colors,
+xterm-256color-na|xterm with 256 colors,
@@ -155,7 +155,6 @@
@@ -170,7 +169,6 @@
$ tic xterm-256color-na # loads the file to $HOME/.terminfo
$ find ~/.terminfo -type f
Now you can test it via:
$ TERM=xterm-256color-na man man # and then hit q
You can repeat the above steps for other terminals if you use multiple ones. If everything works as expected you can set
TERM via your shell rc-file.
You can also directly specify the terminal name as a parameter to
$ infocmp -1 screen > screen
Enable alternate screen switching
Some terminals support the alternate screen switching feature (e.g. screen), but disable it on default.
screen you can enable it via your
$ cat ~/.screenrc
If you remote login via ssh to - say - a Solaris system perhaps the remotely configured system-wide terminfo-database is kind-of historic, broken or includes some unusual/Solaris-specific defaults that don't match the 'bleeding edge' terminals you run ssh in.
Thus, it is an option to just copy the local terminfo database from /usr/share/info (on the client) to
host:~/.terminfo (on the server).
In case the compiled terminfo format is not compatible you can also export/import the entries like in the previous paragraph.
I've tried in with a terminfo-db from a Cygwin installation and copying it directly to a Solaris 10 system worked.
In case you want to share an alternative terminfo db between users you specify its path via the
TERMINFO environment variable.
Ignore the Control Sequence
Some terminals support disabling the control sequence for alternate screen switching. For example xterm has an option, gnome-terminal has not.
For xterm it is the X-ressource, e.g.
Program specific configurations
Some programs have configuration files to configure what control sequence/terminfo commands they send. For example
$ LESS=X less foo
less to not clear/restore the screen.
vim you can include something like
set t_ti= t_te=
I haven't found on option how to configure that behavior for top - by default it does not send restore-screen.