file
:q / :q!
:q closes the current window (or exits Vim if it's the last one); :q! quits and DISCARDS unsaved changes. If you're the 'how do I exit Vim' meme — this is the answer.
:q :q! (command mode) Variations
| Keystroke | Effect |
|---|---|
| :qa | Quit ALL windows / buffers |
| :qa! | Quit all, discard every unsaved change |
| :cq | Quit with error code (useful when Vim is a $EDITOR — aborts the commit/rebase) |
Examples
:q Close current window
:q! Force quit, throw away unsaved edits
:qa! Bail out of an entire Vim session
Gotcha
:q refuses to quit if the buffer is modified — you'll see 'E37: No write since last change'. Use :q! to force it, or :wq to save first.
Related
:wq / ZZ
Write the buffer to disk and quit the window. ZZ (normal mode, two capital Zs) does the same thing without leaving the keyboard's home row — with the subtle difference that ZZ only writes if the buffer actually changed.
:w / :saveas
Write (save) the current buffer to disk. Without an argument :w saves to the current file; with an argument it writes a copy without switching buffers. Use :saveas when you want to rename the buffer to the new file and keep editing that one.
Escape
Leaves the current mode and returns to normal mode. Vim is modal — most of the time you want to be in normal mode; if you're not sure what mode you're in, mash Escape.
:e
Edit (open) a file in the current window. With no argument, :e reloads the current file from disk — handy after external changes.