ubiquibacon
ubiquibacon

Reputation: 10667

How does VIM display to the terminal and then take it back?

How is it that VIM (when running) can display the contents of a file to the terminal, then (when closed) can take what was displayed back? There have been several applications I have made where I would have liked to implement this functionality... like when making a program with terminal graphics where the entire screen typically has to be updated when a single "object" moves.

Upvotes: 7

Views: 1194

Answers (1)

Johan Kotlinski
Johan Kotlinski

Reputation: 25749

It's your terminal that stores the old buffer, not Vim.

If you use XTerm emulation, Vim switches to the "alternate" terminal screen on startup. On exit, Vim switches back to the normal screen.

Terminfo strings at startup:

\E7 saves the cursor's position 
\E[?47h switches to the alternate screen 

Terminfo strings at exit:

\E[2J clears the screen (assumed to be the alternate screen) 
\E[?47l switches back to the normal screen 
\E8 restores the cursor's position. 

Upvotes: 10

Related Questions