How to exit Vim?
Let’s be honest — you’re probably here because Vim won’t let you leave. You’re not alone. “How to exit Vim” is one of the most viewed questions on Stack Overflow, with over 2 million views. The good news: it’s actually simple once you understand what’s going on. Here are the commands, and below that, we’ll explain why Vim works this way.
Quick Exit Commands
Press Esc first to make sure you’re in
NORMAL
mode, then type:
| Command | What it does |
|---|---|
:q |
Quit (fails if you have unsaved changes) |
:q! |
Quit without saving |
:wq |
Save and quit |
:x |
Save and quit (only writes if changes exist) |
ZZ |
Save and quit — no colon needed |
ZQ |
Quit without saving — no colon needed |
“I’m Stuck” — Common Errors
“E37: No write since last change”
You have unsaved changes. Use :wq to save and quit, or :q! to discard changes.
“E212: Can’t open file for writing”
You don’t have write permission. Save with sudo: :w !sudo tee %, then quit with :q!.
Nothing happens when I type :q
You’re probably in
INSERT
or
VISUAL
mode. Press Esc first, then type the command.
Vim Modes Explained
Vim has multiple modes — that’s why quitting isn’t just “close the window.” You need to be in the right mode first.
-
NORMAL
mode (default) — navigate and run commands. Press
Escto get here. -
INSERT
mode — type text. Enter with
i,a, oro. -
VISUAL
mode — select text. Enter with
v,V, orCtrl+v. -
COMMAND
mode — type commands like
:q. Enter by pressing:in NORMAL mode.
All exit commands work from
NORMAL
mode or
COMMAND
mode. If you’re in any other mode, press Esc first.
Want to go deeper?
- Vim Basics — understand modes, buffers, splits, and how Vim actually thinks
- Vim Shortcuts — the cheat sheet you’ll want to bookmark
- Why Vim? — why millions of developers swear by this editor