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.
| Command | What it does |
|---|---|
| Esc then :q | Quit — only works if nothing was changed |
| Esc then :q! | Quit and discard every change |
| Esc then :wq | Save and quit |
| Esc then :wq! | Save and quit, overriding a read-only warning |
| Esc then :x | Save 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.
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
| Key | Action |
|---|---|
| u | Undo the last change |
| Ctrl-r | Redo (undo the undo) |
| U | Undo 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
| Command | What it does |
|---|---|
| :qa / :qall | Quit every open window — fails if any has unsaved changes |
| :qa! | Quit every window, discarding all unsaved changes |
| :wqa / :xa | Save every window, then quit all of them |
A short vim cheat sheet, for getting around
| Key | Action |
|---|---|
| i / a | Insert before / after the cursor |
| Esc | Back to normal mode |
| dd | Delete (cut) the current line |
| yy | Copy (yank) the current line |
| p | Paste after the cursor |
| /text | Search forward for text; n for the next match |
| :42 | Jump to line 42 |
| gg / G | Jump 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.