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.

  • If you always run GNU screen, you will get consistent behavior. – jordanm Aug 3 '13 at 21:47

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 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):

$ echo $TERM
xterm-256color
$ 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,
    am,
    bce,
    ccc,
@@ -155,7 +155,6 @@
    rin=\E[%p1%dT,
    rmacs=\E(B,
    rmam=\E[?7l,
-   rmcup=\E[?1049l,
    rmir=\E[4l,
    rmkx=\E[?1l\E>,
    rmm=\E[?1034l,
@@ -170,7 +169,6 @@
    sgr0=\E(B\E[m,
    smacs=\E(0,
    smam=\E[?7h,
-   smcup=\E[?1049h,
    smir=\E[4h,
    smkx=\E[?1h\E=,
    smm=\E[?1034h,
$ tic xterm-256color-na # loads the file to $HOME/.terminfo
$ find ~/.terminfo -type f
$ HOME/.terminfo/x/xterm-256color-na

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, e.g.:

$ infocmp -1 screen > screen

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:

$ cat ~/.screenrc
altscreen

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.

XTerm*titeInhibit: true

Program specific configurations

Some programs have configuration files to configure what control sequence/terminfo commands they send. For example

$ LESS=X less foo

configures less to not clear/restore the screen.

For vim you can include something like

set t_ti= t_te=

in you ~/.vimrc.

I haven't found on option how to configure that behavior for top - by default it does not send restore-screen.

  • The sed cmd actually delete the whole line containing smcup or rmcup. In my case, there are other stuffs coming on the same line with them. Those other stuffs also got deleted. So maybe something like s/[sr]mcup=[^,]*,//' is better. – Mingliang Nov 14 '16 at 3:24

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.