How do I quit top without leaving the output on the terminal?

When you start it like this ...

top && clear

... the screen clears after exiting top with q.

This acts more like less:

tput smcup ; top ; tput rmcup

(smcup switches to a secondary screen where the top command is executed and when you quit top rmcup switches back to the normal screen)


You could alias the top command to do that ( How do I create a permanent Bash alias? )


Ironically1, this was briefly the behaviour of top. In 3.3.8, top did clear the screen, but not in versions past or since. See Red Hat bug #977561:

The screen clearing is an indirect result of changes in terminal manipulations which were introduced by Jim Warner (author of the 'top' tool) in order to suppress flooding the 'top' tool with SIGWINCH signals generated by some of the widely used terminal applications. It was me who reported the flooding issue and he tried to do his best in order to resolve it and released a fix for the issue in form of 8 separate commits. The commit introducing the screen clearing has the following hash : 'dedaf6e1a81738ff08ee8e8523871e12f555ad6d'.

Due to the bug report, Jim then fixed it to again not clear the output in 3.3.9.

top has no options or configuration settings to control this, AFAICT, this is entirely hardcoded behaviour. You might want to file a feature request at https://gitlab.com/procps-ng/procps/issues/.

Until then, you could clear manually like Rinzwind suggested, or use tput like steeldriver suggests.


Interestingly, the commit mentioned above also has a nice explanation of the command steeldriver suggested, because it is in fact the same as what was originally done in the code:

A scrollback buffer used in several terminal emulators could be a real inconvenience to a user following some resize operations. Extra keystroke(s) would frequently be required in order to properly render top's display.

After much sleuthing we unearthed two terminfo strings which have the effect of disabling/restoring that darn scrollback buffer. They were well hidden under a title of strings 'to start/end programs using cup'. In turn, 'cup' deals with a tty's cursor addressing capability.

We don't care what you call them or what they refer to so long as they get the job done. And these really do! Be advised, however, that there are some side effects, several of which can even be considered as beneficial:

  • enter_ca_mode/smcup/ti disables scrollback buffering ( and that's good, it's what we had always hoped for )

  • exit_ca_mode/rmcup/te restores the scrollback buffer ( but also restores screen contents existing pre-top ) ( which is different from former program end results ) ( where that last rendered screen was left untouched )

... which corresponds to the tput smcup and tput rmcup commands.


1Obligatory xkcd.


The behavior you are looking for from less is built in to an alternative called htop. You can install it with:

sudo apt-get install htop

Specifically, it switching to the alternate screen mode before starting preserving your original screen contents when you close htop. It also has a variety of other enhancements over procps's top. Once it's installed, you can just run it as a regular user:

htop

You could use watch:

watch top -b

It default to update every two seconds, use e.g. -n1 for one second if you want to change this behaviour. To quit watch, press Ctrl+C instead of the usual Q.

Another approach is opening a new terminal window, e.g. with xterm:

xterm -e top

This behaves exactly like top normally does, the window closes when you quit top with Q and of course it leaves no output in the terminal you started it from. Add & to the end of this command to start it in the background and immediately release the terminal again.