Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 old terminal content?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
386 views
Welcome To Ask or Share your Answers For Others

1 Answer

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: <Esc> 7 <Esc> [ ? 47 h
  te: <Esc> [ 2 J <Esc> [ ? 4 7 l <Esc> 8

xterm-256color
  ti: <Esc> [ ? 1 0 4 9 h
  te: <Esc> [ ? 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...