Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

terminal

vim

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.

like image 224
ubiquibacon Avatar asked Dec 22 '10 22:12

ubiquibacon


People also ask

How do I temporarily disable Vim?

TL;DR – How to Exit Vim If you didn't make any changes, type :q and press Enter / return. If you made some changes and would like to keep them, type :wq and press Enter / return. If you made some changes and would rather discard them, type :q! and press Enter / return.

How do I open a Vim file in the background?

If you quickly want to switch to your shell, suspend the Vim editor with Ctrl+z . That sends the process into the background (on Linux). Now you have access to your standard terminal and can run commands.


1 Answers

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. 
like image 98
Johan Kotlinski Avatar answered Nov 12 '22 23:11

Johan Kotlinski