This is discussed at length in the xterm FAQ Why doesn't the screen clear when running vi?.
ncurses as such doesn't do it. This is done by the terminal, given the escape sequences provided in the terminal description.
The behavior is from the alternate screen buffer, which was introduced with xterm in X10R4 (the end of 1986). xterm maintains two buffers:
- the normal screen buffer (what you see in the VT100 window) including the scrollback area, and
- the alternate screen buffer (which does not include the scrollback area)
xterm (and programs which use this feature, such as rxvt) recognizes escape sequences for switching between the two buffers. Since conventional termcap had no feature for this, the developers used the termcap initialization sequences ti
and te
(which correspond to terminfo smcup
and rmcup
). Technically the names refer to cursor-addressing mode, but no one cares (much).
The original xterm's alternate screen feature could be suppressed by removing at runtime the termcap ti
and te
capabilities (from the TERMCAP
environment variable), but that approach could not work with terminfo, which uses files. Since 1997, xterm has implemented a newer set of escape sequences which can be ignored by xterm. A few other programs allow the feature to be suppressed, but as a rule, those do not.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…