Depending on the terminal/environment following sequence has different effects:
$ ls
1
2
$ man ls
enter q
Either I see last displayed man-page screen above the current prompt or the ls output and previous shell output is restored. Same effect is observed e.g. when using vim
and then suspending it to do something on the shell.
On Fedora 19 – the default of screen seems to be no-restore, a gnome-terminal/xterm uses do-restore by default.
On Solaris it depends on the used terminal/terminfo db it seems.
I want to configure the restore behavior consistently between different systems/terminals.
Best Answer
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 anxterm
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.:The last two commands also work in gnome-terminal (probably in others, too).
What happens when
man
/vi
/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):Now you can test it via:
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
, e.g.:Enable alternate screen switching
Some terminals support the alternate screen switching feature (e.g. screen), but disable it on default.
For
screen
you can enable it via your~/.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
configures
less
to not clear/restore the screen.For
vim
you can include something likein you
~/.vimrc
.I haven't found on option how to configure that behavior for top - by default it does not send restore-screen.