How does vi restore terminal content after quitting it?

后端 未结 2 2040
名媛妹妹
名媛妹妹 2020-12-01 18:55

How does a program like vi or man or any other program replace the terminal content with the program\'s own contents then after quitting those programs they bring back the o

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 19:47

    Vi flips to the alternate screen buffer, supported by terminals. This is achieved using escape sequences. See this link for full details.

    The termcap entry for these are 'ti' to enter, and 'te' to exit full-screen mode.

    As @Celada points out below, hardcoding xterm escape sequences is not a Good Idea™, because the sequences vary according to $TERM, for example:

    xterm-color
      ti:  7  [ ? 47 h
      te:  [ 2 J  [ ? 4 7 l  8
    
    xterm-256color
      ti:  [ ? 1 0 4 9 h
      te:  [ ? 1 0 4 9 l
    

    On the other hand, xterm support is very broad these days among non-xterm terminals. Supporting only xterm is unlikely to cause problems, except for users with exotic or obsolete $TERM settings. Source: I support products that do this.

提交回复
热议问题