When you enter vim, it "clears" the screen. Upon exiting, it "restores" the original contents.
I understand one can use \x1b[2J
to clear the console and reset the cursor position, but this will overwrite terminal contents.
I assume Vim uses ncurses under the hood, in which case I suppose the better question is how ncurses does this, but how is it done?
Most terminal emulators are able to save and restore the contents of the screen.
The terminfo codes for this are smcup
to enter full-screen mode and rmcup
to leave it. (The older termcap codes are ti
and te
.)
If these capabilities are enabled in the terminfo
database, any program that uses ncurses
will print the smcup
string on entry, and the rmcup
string on exit.
On the system I'm using at the moment, the strings are (with \E
representing the Escape character):
smcup: \E7\E[?1;47h
rmcup: \E[2J\E[?1;47l\E8
This restores the previous contents of the screen as well as the cursor position.
The specific meanings of the sequences (for xterm) are documented here:
- smcup:
\E7
Save Cursor
\E[?1;47h
Application Cursor Keys; Use Alternate Screen Buffer
- rmcup:
\E[2J
Erase screen
\E[?1;47l
Application Cursor Keys; Use Normal Screen Buffer
\E8
Restore Cursor
(This assumes I'm understanding the use of the semicolon correctly; I'm not 100% sure of that.)
Regarding the answer by @Keith Thompson — not exactly:
- vim does not use the screen optimization of ncurses which sends
smcup
and rmcup
automatically. Rather, it is a termcap application. It follows a convention used by most (not all) termcap applications. There are some implementations of vi
which do not for instance (on IRIX64 perhaps).
- as for "most terminals" — actually, xterm look-alikes are a small part of the terminal database (even counting variations, less than 10%). Rephrase that to something like "the most common terminal emulators on Linux.
- the terminal does not save and restore the screen contents. Instead, it switches between two screens (in xterm's documentation "normal" and "alternate"). In xterm for instance, one can always switch between the two using a menu entry. The xterm FAQ Why doesn't the screen clear when running vi? gives more detail.
- for better context, note that
smcup
is an (obscure) abbreviation for set-mode-cursor-positioning, or start cursor-positioning mode. (also cursor-addressing). The r
in rmcup
means "reset" (and m
means "mode"). set/reset have different connotations from save/restore; with the latter the user is led to believe that the values can be stacked up.