Quick Answer: To save a file in Vi/Vim and exit, press Esc to enter normal mode, then type :wq and press Enter. You can also use :x or ZZ to save and quit. To quit without saving, type :q! and press Enter.
The Fast Answer (Save and Exit Vim)
- Press Esc to make sure you’re in normal mode.
- Type
:wq(write & quit). - Press Enter.
That’s it. If :wq doesn’t seem to work, you’re almost always still in insert mode — press Esc first, then try again.
Why You Can’t Just Type to Save (Vim Modes)
Vim has different modes, and this trips up almost every beginner:
- Normal mode — for commands (saving, quitting, navigating). Vim starts here. Press Esc to return to it any time.
- Insert mode — for typing text. You enter it with
i,a, oro. - Command-line mode — the line at the bottom that starts with
:, where you type commands like:wq.
Save and quit commands only work from normal mode, which is why you press Esc first.
All Save & Quit Commands in Vi/Vim
| Command | What it does |
|---|---|
:w | Save (write) the file, stay in Vim |
:wq | Save and quit |
:x | Save and quit (only writes if there are changes) |
ZZ | Save and quit (no colon needed — just press Shift+Z twice) |
:q | Quit (only works if there are no unsaved changes) |
:q! | Quit and discard all unsaved changes |
:wq! | Force save and quit (e.g. read-only file you own) |
ZQ | Quit without saving (no colon needed) |
:w filename | Save as a new file name |
:wa / :wqa | Save all / save all and quit (multiple files) |
Save Without Exiting
To save your work but keep editing, press Esc, then type :w and press Enter. The file is written to disk and you stay in Vim.
Save and Exit
Any of these save the file and close Vim (press Esc first):
:wq # write and quit
:x # write (if changed) and quit
ZZ # write and quit — shortcut, no Enter neededQuit Without Saving (Discard Changes)
If you made changes you don’t want to keep, press Esc and type:
:q! # quit and throw away unsaved changes
ZQ # same thing, shortcutSave a Read-Only File (Permission Denied)
If you opened a system file without sudo and get “E45: ‘readonly’ option is set”, you can still save it with this trick:
:w !sudo tee %
# then type your password, and :q! to exitOr force-save a file you own that’s marked read-only with :wq!.
Help! I’m Stuck in Vim
The classic situation. To get out:
- Press Esc a couple of times (this exits insert mode and cancels any half-typed command).
- To leave and keep changes: type
:wqand Enter. - To leave and discard changes: type
:q!and Enter.
Vi vs Vim — Does It Matter?
No — all the commands above work the same in both vi and vim (Vim is “Vi Improved”). On most modern Linux systems, typing vi actually launches Vim. Master these commands and you can edit files on any Linux server.
Want more command-line skills? See our complete guide to Linux file editors (nano, vi, vim), Essential Linux Commands, and Linux for DevOps Engineers.
Frequently Asked Questions
How do I save and exit Vim?
Press Esc, type :wq, and press Enter. Alternatives are :x or ZZ.
How do I quit Vim without saving?
Press Esc, type :q!, and press Enter to discard all changes and exit.
What is the difference between :wq and :x?
Both save and quit. :wq always writes the file (updating its timestamp); :x only writes if there were actual changes.
Why won’t :wq work?
You’re almost certainly still in insert mode, so the characters are being typed into the file. Press Esc first, then type :wq.

