How to exit Vim
Press Esc, then type :q! and ↵ to quit without saving, or :wq and ↵ to save your changes and quit.
Everything that ends Vim starts in Normal mode, the mode where keys are commands. If the bottom line says -- INSERT --, the keys you type go into the file, so :q! would just add the letters :q! to your text. Press Esc first (pressing it twice does no harm), then type the command: it appears on the bottom line as you type it, and ↵ (the Enter key) runs it.
| Type | What it does |
|---|---|
:q ↵ | Quit. Refuses if the file has unsaved changes. |
:q! ↵ | Quit and throw the changes away. |
:wq ↵ | Save (write) the file, then quit. |
:x ↵ or ZZ | Save only if something changed, then quit. ZZ needs no Enter. |
| ZQ | The same as :q!, in two keys. |
:w ↵ | Save without quitting. |
:qa ↵ / :qa! ↵ | Quit every window and tab at once; the ! discards changes in all of them. |
:wqa ↵ | Save every file, then quit. |
:cq ↵ | Quit with an error code. A program that opened Vim for you (Git, for example) treats it as "cancel". |
Practise the way out
Type the keys for each one, as you would in Vim. Nothing here quits anything.
:wq or :x?
Both save and quit. :wq always writes the file, even when nothing changed, which updates its modification time; :x (and ZZ) write only if there is something to write. Tools that watch files for changes, like a build that reruns on save, notice the difference; for everything else they are the same.
Stuck in Vim from Git
git commit without -m, git rebase -i and git merge open your editor, often Vim, and wait for it to close. Write the message, then :wq to go ahead. To cancel instead, :cq quits with an error and Git stops (for a commit, quitting with an empty message does the same).
If you'd rather Git used another editor, tell it once: git config --global core.editor "code --wait" for VS Code, or nano.