Guides

How to exit vim: :wq, :q! and every other way out

Esc then :wq saves and quits; :q! quits without saving. Every exit command, why vim traps people in the first place, and how to change the default editor.

Published ~6 min read

Short answer: press Esc to make sure you are out of insert mode, then type :wq and press Enter to save and quit, or :q! to quit without saving. If the file has not changed, plain :q works too. All of these are typed commands, not key combinations — they appear at the bottom-left of the screen as you type them.

CommandWhat it does
Esc then :qQuit — only works if nothing was changed
Esc then :q!Quit and discard every change
Esc then :wqSave and quit
Esc then :wq!Save and quit, overriding a read-only warning
Esc then :xSave and quit, but only writes the file if something actually changed
ZZ (capital, no colon)Same as :wq — save and quit, in normal mode
ZQ (capital, no colon)Same as :q! — quit and discard changes

Why people get stuck in vim

vim opens in normal mode, where keystrokes are commands, not text — typing :wq while still in insert mode (entered with i, a, o and a few others) just types the literal characters :wq into the file instead of running them as a command. Esc always returns to normal mode first, from anywhere, which is why every sequence above starts with it.

vim ends up open by accident more often than on purpose — it is the default editorgit commit opens for a commit message, and the default EDITOR on many minimal server images, including inside crontab -e and visudo.

Saving and quitting: :wq, :x, ZZ

:w writes (saves) the file without quitting, :q quits without saving (only when there is nothing unsaved to lose), and :wq is the two in sequence. :x behaves like :wq but skips writing the file if it was never actually changed, which matters only for preserving the file’s modification timestamp.

Normal mode, no colon needed
ZZ    " save and quit — same as :wq<Enter>
ZQ    " quit without saving — same as :q!<Enter>

Quitting without saving: :q!

vim refuses a plain :q the moment anything has changed, with E37: No write since last change — that message is vim protecting you from losing work, not an error to work around with a different key. Add the ! once you are sure: :q! discards every change since the file was opened and exits.

Undo and redo, before you decide to quit

KeyAction
uUndo the last change
Ctrl-rRedo (undo the undo)
UUndo every change on the current line (old-school, rarely needed now)

If you opened the file, made a mess and just want your original back, u repeatedly is usually faster and safer than :q! followed by reopening it — it gets you back to the unmodified state without losing the ability to redo if you undid one step too many.

Quitting multiple splits or tabs

CommandWhat it does
:qa / :qallQuit every open window — fails if any has unsaved changes
:qa!Quit every window, discarding all unsaved changes
:wqa / :xaSave every window, then quit all of them

A short vim cheat sheet, for getting around

KeyAction
i / aInsert before / after the cursor
EscBack to normal mode
ddDelete (cut) the current line
yyCopy (yank) the current line
pPaste after the cursor
/textSearch forward for text; n for the next match
:42Jump to line 42
gg / GJump to the start / end of the file

Specifically: stuck in a git commit message

Running a bare git commit opens vim with a commented template. Type i, write the message, Esc, then :wq — or leave the file untouched and :q to abort the commit entirely, which git treats the same as an empty message.

visudo, used when setting up a sudo user, opens the same way and the same :wq / :q! rules apply — it just validates the syntax before saving and refuses to write a broken file.

For the rest of a server’s everyday commands beyond the editor, see the Linux commands cheat sheet.

Frequently asked

How do I exit vim?

Press Esc to make sure you're in normal mode, then type :wq and press Enter to save and quit, or :q! to quit without saving. ZZ (no colon) does the same as :wq; ZQ does the same as :q!.

Why won't vim let me quit with :q?

Because the file has unsaved changes — vim refuses with 'E37: No write since last change' to stop you losing work. Save with :wq, or discard the changes on purpose with :q!.

Why did typing :wq just add those characters to my file?

You were still in insert mode, where keystrokes are typed as text rather than run as commands. Press Esc first to return to normal mode, then type :wq again.

How do I stop accidentally opening vim?

Set a different default editor: export EDITOR=nano in your shell's profile, and git config --global core.editor "nano" for git specifically. vim stays installed — it just stops being what opens first.

Related articles

All guides