What mechanism allows ViM to temporarily overwrite the entire console?

前端 未结 2 863
谎友^
谎友^ 2021-01-12 01:37

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 consol

2条回答
  •  花落未央
    2021-01-12 01:59

    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.)

提交回复
热议问题