This has been annoying me for ages. Even though I use a modern terminal emulator and most CLI programs, such as
vim, are able to use the alternate screen adequately (i.e. entering it on startup and leaving it on exit), some are reluctant to do so. Rogue programs include
When they start, these programs clear the contents of the window (i.e. the last N lines where N is the height of the terminal window). When they exit, some (
top) leave their last state visible, while others (
screen) clear the screen again. In all cases, the last N lines of scrollback buffer have been overwritten.
I checked this on various machines over time, and in various terminal emulators, including
screen (the alternate screen of which being properly enabled, using
:altscreen on). Hence I do not think it is a problem with my terminal, I rather believe it is the built-in behavior of these programs (at least unpatched, as they are distributed in Archlinux).
So my questions are:
- Why do these programs behave like this? I guess there are good reasons for it?
How can I work around it? Currently, I am using dumb wrapper scripts like the one below, but maybe there is a cleaner way?
# save this as: ~/bin/top if [ -t 1 ]; then # only use alt screen if output is a terminal tput smcup # toggle alt screen on /usr/bin/top "$@" tput rmcup # toggle alt screen off else /usr/bin/top "$@" fi