Some programs like vim/nano/nload has the ability to use the whole terminal to display its content and upon exiting, the previous terminal output is restored. How do they implement this restoring process?
1 Answers
How do they implement this restoring process?
They don't. It's not theirs to implement.
This is the so-called alternate screen buffer being switched in and out. It's implemented in the terminal (or terminal emulator program) itself. The terminal responds to control sequences that are emitted by such TUI programs. The TUI programs control when this happens, but the implementation of what happens is entirely within the terminal. Indeed, the TUI programs don't even have the same model of what is happening. To them, they are switching into and out of "cursor addressing mode".
Not all terminals and terminal emulator programs even have an alternate screen buffer. For example: the terminal emulator programs built into Linux and BSD kernels that provide their kernel virtual terminals usually don't have this capability.
On such terminals, there is no control sequence. The termcap/terminfo database record for the terminal type will, consequently, not have such control sequence; and switching to and from "cursor addressing mode" overwrites the current screen content without saving and restoring it.
vim, nano, and whatnot have no knowledge of this. They aren't doing anything different. They aren't what performs the function. They are simply emitting the control sequences that termcap/terminfo tell them will enter and exit "cursor addressing mode". For some terminals, that have the mechanism, "cursor addressing mode" also means use the alternate screen buffer. For some, that have not, it does not.
Further reading

- 544,893

- 68,745
printf \\33\[\?1047h
prints thexterm
private mode escape sequence for switching to the alternate screen buffer (which does not store a scrollback) andprintf \\33\[\?1047l
swaps back. – mikeserv Nov 16 '15 at 02:30stty
affects thetermios
structure of the kernel-managed serial device. any changes made that way aren't directly related to what the user-space terminal program does. doingstty -echo;printf \\33\[\?1047h
will still result in no output when you press keys - the terminal program (likexterm
) is reading input from the [pt]ty device and writing it to the screen. it intercepts some escapes and performs certain functions, but not at the same levelstty
changes do. – mikeserv Nov 16 '15 at 02:44trap
and terminal escapes cannot be usefully combined, of course. they are very handy when paired, actually. – mikeserv Nov 16 '15 at 02:47xterm
's emulation of the console. In that way I thought the stty settings might apply as the questioner mentioned "use the whole terminal to display".kkk – RobertL Nov 16 '15 at 05:27ex
is spec'd for canonical input, andvi
for raw-mode input. but they don't use shell traps - and they don't affect the terminal throughstty
- they maintain their own local copies of the global structures. and anyway, that stuff is hard. the canonicalvi
- as you can see well-documented here, might use t[ci] libs, and might not, but will try to do the right thing always. in any case, they don't do it with escape codes - they're almost always ioctls. – mikeserv Nov 16 '15 at 05:35vi
can send escape codes, via the ncurses library (libtinfo5). I don't know whatncurses
sends to an xterm. So vi does this: savetermios
state (stty -g
), changes termios state to raw (stty raw
), usesncurses
routines,tput
for example, to control the full screen display. When done,vi
restores the termios to the default line input state (with the results ofstty -g
).vi
doesn't use shell traps and commands, but calls the same underlying system calls as the shell andstty
. The model is almost the identical. – RobertL Nov 16 '15 at 06:40