TL;DR
The content clearing behavior is caused by the smcup/rmcup capability. Here’s the
methods to preserve screen content depends on what terminal you use:
For
tmux, thealternate-screenoption defaults toon. Add the following in.tmux.confto disable it:set-window-option -g alternate-screen offscreendoes not enable the annoying terminal capability by default. If you encounter this, add the following to.screenrc:alterscreen offIf you don’t use
screenortmux, I suggest you invest some time in these tools and I assure you they worth it. If you can’t use these tools, then follow the directions in smcup/rmcup: hate. This removes the declaration of the annoying capabilities in theterminfodatabase, so the application will not use them.
Update: If you find ssh “clears” screen after logout, check if .bash_logout
on the server contains clear command.
Why?
When working on terminal, I prefer applications such as less and vim
preserve their content on exit. This is quite handy to me. For example I often
look up some commands’ man page for some options and then started typing on the
terminal. With the man page preserved on the screen, I can still look it up when
typing. (Note that man send its output to $PAGER, which is less for me.)
I’ve noticed that the content preserving behavior is different on different
machines, and it also varies if you use screen or tmux. After arguing with
one member in my lab which behavior is better, I finally decided to figure out
what caused the difference.
Behind the scene
So let’s first figure out how is the clear screen behavior implemented. (More
actually, restore to what the screen looks like before calling the command.)
tmux’s man page explains what the alternate-screen option does:
This option configures whether programs running inside tmux may use the terminal alternate screen feature, which allows the smcup and rmcup terminfo(5) capabilities.
If we set this option on, less and vim will first store the content on the
screen and restore the content after exit. (You can disable this for less with
the -X option.)
The man page of terminfo is not very clear on what smcup/rmcup does.
Here’s an example of using smcup/rmcup to save and restore screen,
copied and modified from
bash-hackers. I’m using
git log as an example here (you can also try more the file itself, but
the effect is not that obvious):
1 2 3 4 5 6 7 8 9 10 11 | |
How the solution works
For application to save and restore screen, 2 conditions must be met:
- The terminal supports
smcup/rmcupcapability (such as iTerm2 on OS X) - The
terminfoset by$TERMdeclares these capability (xtermdeclares,vt100does not. But the above example still works even if$TERMis set tovt100. I guesstputstill sends out the correct control code even though thevt100 terminfoset by$TERMdoes not declare it. But applications should first check the capability of terminal. and only send control-code if that capability exists.)
Breaking either of the conditions can preserve the screen content. That’s how the solution in the TL;DR section works.