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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With