How Does Vi Restore Terminal Content After Quitting It

How does vi restore terminal content after quitting it?

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.

Keeping view of edited file after exiting vim

Vim uses a terminal feature called the alternate screen to write its UI there, and restore the original shell contents (where Vim was launched from) on exit. This is controlled by two ANSI escape sequences (see how does vi restore terminal content after quitting it).

You can disable that from within Vim by clearing the corresponding terminal settings. Put the following into your ~/.vimrc:

set t_ti= t_te=

Alternatively, you could also disable this capability in the terminal; at least the multiplexers screen and tmux allow this. See Prevent Applications Like Vim and Less Clearing Screen on Exit for details.

How do I exit Vim?

Hit the Esc key to enter "Normal mode". Then you can type : to enter "Command-line mode". A colon (:) will appear at the bottom of the screen and you can type in one of the following commands. To execute a command, press the Enter key.

  • :q to quit (short for :quit)
  • :q! to quit without saving (short for :quit!)
  • :wq to write and quit
  • :wq! to write and quit even if file has only read permission (if file does not have write permission: force write)
  • :x to write and quit (similar to :wq, but only write if there are changes)
  • :exit to write and exit (same as :x)
  • :qa to quit all (short for :quitall)
  • :cq to quit without saving and make Vim return non-zero error (i.e. exit with error)

You can also exit Vim directly from "Normal mode" by typing ZZ to save and quit (same as :x) or ZQ to just quit (same as :q!). (Note that case is important here. ZZ and zz do not mean the same thing.)

Vim has extensive help - that you can access with the :help command - where you can find answers to all your questions and a tutorial for beginners.

How does console/shell caching/restoration work?

The terminal feature described in the question the alternate screen feature (originally xterm, but copied/imitated by several other terminals including konsole). Depending on the terminal description, you may/may not use this feature.

less and most full-screen terminal programs such as vi send the escape sequences to switch to/from alternate screen if they're defined in the terminal description (i.e., TERM=xterm).

From the description, it sounds as if you're using different tabs in the same instance of konsole, and that it's remembering that your terminal was set to the alternate screen. konsole and some other programs attempt to save/restore "session" information when halted, so that probably seemed like a good thing to save/restore.

While in the alternate screen, terminal programs typically have little or no access to the scrollback region.

You can use tput to send the same escape sequence (without running less):

tput rmcup

(If the terminal description doesn't have a definition for that, it'll do nothing).

Further reading:

  • Why doesn't the screen clear when running vi?

When using vim or less in gnu screen, quitting vim or less leaves a lingering imprint

screen's altscreen (alternate screen) feature is turned off by default.

Add this to your .screenrc:

altscreen on

See: http://www.gnu.org/software/screen/manual/screen.html#Redisplay



Related Topics



Leave a reply



Submit