Home / Localhost / Exit Vim

How to exit Vim: :q!, :wq and why it will not quit

Get out of Vim: press Esc, then :q! to quit without saving or :wq to save and quit. Also why you can get stuck and how to recover a file.

Vim has modes. Typing :q does nothing while you are typing text, because commands only work in Normal mode. Press Esc first, then type the command and press Enter.

Get out of Vim, step by step

  1. Press Esc once, sometimes twice, to reach Normal mode.
  2. To leave without saving, type :q! and press Enter.
  3. To save and leave, type :wq and press Enter.
  4. If Vim says there are unsaved changes, pick one of the two commands above.

Commands you can copy

Quit, discard changes
:q!
Save and quit
:wq
Save only if changed, then quit
:x
Quit (fails with unsaved changes)
:q
Normal mode: same as :x
ZZ
Normal mode: same as :q!
ZQ

Why Vim will not quit

Nothing happens when you type :q
You are probably in Insert mode, so the characters are being typed into the file. Press Esc first. Make sure the English input method is active, or the colon may not register.
E37: No write since last change
:q refuses to quit when the file has unsaved changes. Use :wq to keep them or :q! to throw them away.
The file is read-only or has no name
:wq can fail for these reasons, and even :wq! may fail on a read-only file. Save a copy elsewhere with :w ~/my-backup-file and move it afterwards with mv, or use :w !sudo tee % to write it with sudo.
Ctrl+C does not quit
In Vim, Ctrl+C only shows a message. If the terminal is frozen you may have to close it, which can leave a .swp swap file. When you open the file again Vim offers recovery; press R to recover your changes.
I opened Vim by accident
Git, crontab and some other commands open a text editor, which may be Vim on your system. Quit with :q!, then set another editor, for example git config --global core.editor nano. See the gitconfig generator.

Questions about Exit Vim

What is the difference between :wq and :x?
:wq always writes the file and quits. :x writes only if there are changes, so it leaves the file's modified time alone otherwise. ZZ in Normal mode does the same as :x.
How do I quit without saving?
Press Esc, then type :q! and press Enter. You can also type ZQ in Normal mode.
Why does :q not work?
Either you are not in Normal mode (press Esc) or the file has unsaved changes.
Can I recover a file after closing the terminal?
Often yes. Vim leaves a .swp swap file, and reopening the file offers a recovery prompt.

Updated 2026-10-27

Related pages