To exit Vim, press Esc, then type :wq and press Enter to save and quit, or type :q! and press Enter to quit and throw away your changes. If Vim answers with E37: No write since last change, you have unsaved edits and must choose one of those two.
Every command below starts with a colon and ends with Enter. The colon only works in Normal mode, which is why Esc comes first.
The exit commands
| Type this (after Esc) | What it does |
|---|---|
:wq | Write (save) the file, then quit |
:x or ZZ | Save only if there are changes, then quit |
:q | Quit; refuses if there are unsaved changes |
:q! or ZQ | Quit and discard unsaved changes |
:qa! | Quit every open file and window, discarding all changes |
:wqa | Save all changed files and quit |
:cq | Quit without saving and return an error code (useful for aborting git) |
ZZ and ZQ are typed in Normal mode without a colon and without Enter; they are capital letters, so hold Shift. Vim's own help defines ZZ as "same as ":x"" and ZQ as "Quit without checking for changes (same as ":q!")".
Step by step, when you're stuck
- Press
Esc, once or twice. The Vim user manual's advice for mode confusion is: "To get back to Normal mode, no matter what mode you are in, press the <Esc> key", and it notes that sometimes you have to press it twice. If Vim beeps, you were already in Normal mode. - Type
:. The colon appears at the bottom-left of the screen, with the cursor after it. If it doesn't, you are still in Insert mode; pressEscagain. - Type
wqorq!and pressEnter.
If your Esc key is awkward to reach or remapped, Ctrl+[ sends the same key code in a terminal. A -- INSERT -- label at the bottom of the screen means you are in Insert mode; it disappears when you return to Normal mode.
What Vim's exit errors mean
The message appears on the bottom line. All except E162 were reproduced with Vim 9.1; the E162 wording is from Vim's help file message.txt.
| Message | Why | What to type |
|---|---|---|
E37: No write since last change (add ! to override) | You typed :q with unsaved edits | :wq to keep them, :q! to discard |
E162: No write since last change for buffer "notes.txt" | Another open file has unsaved edits | :wqa to save all, :qa! to discard all |
E32: No file name | You typed :wq in a new, unnamed buffer | :wq notes.txt to save under a name, or :q! |
E45: 'readonly' option is set (add ! to override) | The file was opened read-only (for example with view or vim -R) | :wq! if you have write permission, otherwise see below |
E212: Can't open file for writing | Your user can't write to that file or folder | Save a copy elsewhere, see below |
E173: 1 more file to edit | You opened several files and haven't visited the last | Repeat the quit command; the second attempt works |
E492: Not an editor command: Wq | Typo or wrong case | :wq in lower case |
The help text for E37 sums up the design: "Vim protects you from losing your work. You can either write the changed file with ":w", or, if you are sure, abandon it anyway". The ! is how you say you are sure.
Saving a file you don't have permission to write
If you opened a system file such as /etc/hosts without sudo, :wq fails with E212 or E45. Don't lose your edits: save a copy somewhere you can write, quit, then move it into place with elevated rights.
:w /tmp/hosts.edited
:q!
Then in the shell:
sudo cp /tmp/hosts.edited /etc/hosts
Next time, open protected files with sudoedit /etc/hosts or sudo vim /etc/hosts.
Exiting Vim inside git commit (and aborting the commit)
Git opens an editor for commit messages, merges and interactive rebases. If you never configured one, it is often Vim: the git var docs say Git uses $GIT_EDITOR, then core.editor, then $VISUAL, then $EDITOR, and finally "the default chosen at compile time, which is usually vi".
- To commit: press
i, type your message on the first line, pressEsc, then:wq. - To cancel a commit:
:q!without typing a message. Git printsAborting commit due to empty commit message.and nothing is committed. - To cancel anything Git opened (rebase todo, merge message):
:cq. Vim exits with an error code and Git stops, printingerror: there was a problem with the editor 'vim'.
Both messages above are what Git 2.45 printed in a test repository; after the :cq error it also prints Please supply the message using either -m or -F option. To avoid Vim in the future, set another editor once:
git config --global core.editor "nano"
What doesn't exit Vim
Ctrl+Ccancels the current command but leaves Vim open. Vim 9 shows a hint on the bottom line instead:Type :qa and press <Enter> to exit Vim, or, when there are unsaved changes,Type :qa! and press <Enter> to abandon all changes and exit Vim.Ctrl+Zin a Linux or macOS terminal suspends Vim to the background. You get your shell back, but Vim is still running with the file open. Typefgto return to it, then quit properly.- Closing the terminal window ends Vim abruptly. Unsaved changes are not written to the file, and Vim usually leaves a swap file (on Linux and macOS,
.notes.txt.swpnext to the file) behind, so the next time you open the file it warns about it.
If Vim still won't close
If the bottom line shows : but nothing happens, check you pressed Enter. If keys produce odd characters, your terminal may be sending a different code for Esc; use Ctrl+[. If you see a split screen, you have several windows open: :qa! closes all of them. If :q is answered with a swap-file warning about another Vim session, that other session still has the file open; find it and quit it there. For a simpler editor where the shortcuts are listed on screen, see how to use nano, and for other everyday terminal commands, basic Linux commands.
Frequently asked questions
- What is the difference between :wq and :x?
- Both save and quit. :wq always writes the file, even if nothing changed, which updates its modification time. :x (and ZZ) writes only when there are unsaved changes. In everyday use they do the same thing.
- Why does typing :q just insert the text :q into my file?
- You are in Insert mode, so keys are typed as text. Press Esc first to return to Normal mode, then type :q. If you are unsure which mode you are in, press Esc twice; pressing Esc in Normal mode only beeps, it does no harm.
- How do I stop Git from opening Vim?
- Set a different editor with git config --global core.editor "nano" (or the command for any editor you prefer), or pass the message directly with git commit -m "message". Git checks GIT_EDITOR, then core.editor, then VISUAL, then EDITOR, and falls back to its compile-time default, usually vi.
- Is Ctrl+C or Ctrl+Z a way to exit Vim?
- No. Ctrl+C in Vim's Normal mode does not quit; Vim shows a hint to type :qa and press Enter instead. Ctrl+Z in a Unix terminal suspends Vim into the background rather than closing it, and the file stays open until you bring it back with fg and quit properly.