Alternate Screen in Terminal Emulator

Terminal (short for Terminal Emulator) is the key interface between developers and Linux, and we spend most of our working there. Because of such close interaction, we (maybe only me) tend to forget the complexity of it, believing it’s just one simple tool, does some simple jobs. Unfortunately, that’s far from the truth; most terminals are unbelievably complex, mainly because of history reasons. Even though I use it in my daily work, I have basically no idea what’s happening inside it. I will only cover one feature, alternate screen, of terminal in this post. When I learned more about terminal, I will wrote them as well.

What THE HELL is Alternate Screen

Alternate Screen could be understood as one alternative workspace (buffer or universe), and everything happens there will not affect the main screen, (the one you see or in before entering Alternate Screen, and after exiting it). Considering one might looks through the history buffer of the terminal, it makes sense that the history is kept in one readable fashion. Some screen-oriented programs could easily messed it up, if there’s no Alternate Screen. (It’s one design choice, based on the assumption that people will read the history buffer sometime, and it’s one reasonable assumption, IMO.)

It’s still a bit abstract…

I agree. Maybe this will make it clear if the process of entering and exiting Alternate Screen is illustrated. Terminal will enter and exit Alternate Screen only when it’s requested to, and the correspond command is tput smcup and tput rmcup, respectively. (I admit, it’s rather cryptic.) The programs need to use Alternate Screen will use these two commands to request for it, which happens behind the scene.

Come on, you didn’t explain it

You just introduce some cryptic commands, that claims to do the work. I agree, again. tput is just one tool, that has sth to do terminfo, which provides some description for the terminal’s capability, for different terminals have different capability (you can’t imagine how fragmented the open source software world is). Therefore, terminfo acts as one abstract layer over all the different terminals. Inside terminfo, smcup and rmcup are just two entries describing the capability concerning Alternate Screen. (I could find what they stand for, so I persuade myself that smcup stands for Save Memory CUrsor Positon, and rmcup could be deducted easily then).

It starts to make sense now, but terminfo is still quite abstract

Not if you can see the content of terminfo, using infocmp, which will show the terminfo for the terminal you are using.