This has been annoying me for ages. Even though I use a modern terminal emulator and most CLI programs, such as less
and 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 top
, screen
and dialog
.
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 xcfce4-terminal
, urxvt
and 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
altscreen on
in my~/.screenrc
I don't have such an issue. Have you considered updating the version ofscreen
that's installed? Seems like this is more than one question, also. – Wildcard Aug 02 '19 at 23:13screen
’s “inner” configuration specifically. – Maëlan Aug 03 '19 at 00:04USE_ALTSCREEN
of which I would have never heard before… would qualify. – Maëlan Aug 03 '19 at 00:34initscr()/endwin()
) don't have to do anything special; the ncurses library takes care of everything. – Aug 03 '19 at 02:17/usr/bin/top &
fails with the error message “failed tty set: Interrupted system call”). So I guess I will stick with this partial solution. Even thoughtop_wrapper.sh &
has strange effects… – Maëlan Aug 07 '19 at 12:08waitpid()
to determine when it stops and continues. – Toby Speight Aug 09 '19 at 07:34