Stuck in Vim? Press Esc first, then type one of these and press Enter:
:wq save and exit
:q! quit WITHOUT saving
:w save but stay in Vim
That covers ninety percent of cases. The rest of this guide explains why the editor seemed to ignore your typing, the difference between the exit commands, and how to recover work you thought was lost.
Why nothing happens when you type
Vim is a modal editor. In insert mode your keys type text; in normal mode they are commands. Commands that start with a colon only work from normal mode, and pressing Esc always gets you there. When in doubt, press Esc once or twice, then type the command. If you see the colon appear in the bottom left corner, you are in the right place.
All the ways to save and quit
:wwrites the file and keeps Vim open:wqwrites, then quits:xwrites only if there are changes, then quitsZZ(capital, no colon) same as:x:qquits, but refuses if there are unsaved changes:q!quits and discards changesZQ(capital, no colon) same as:q!:wawrites all open buffers;:wqawrites all and quits
The practical difference between :wq and :x: :wq always writes, updating the file modification time even if nothing changed, while :x writes only when the buffer was modified. On a build system that watches timestamps, that distinction can matter.
Saving under a different name
:w newname.txt
This writes a copy to newname.txt but keeps editing the original file. To continue editing the new file instead, use :saveas newname.txt.
When the file will not save
E45: readonly option is set: you opened the file with view or with -R, or the file mode is read only for you. Force the write with:
:w!
Permission denied even with :w!: you edited a root owned file as a normal user, a classic with files under /etc on a Linux VPS. Instead of losing your edits, write the buffer through sudo:
:w !sudo tee %
Vim pipes the buffer to tee running as root; % stands for the current filename. Confirm the prompt, then press L to reload. Next time, open the file with sudo vim /etc/whatever or use sudoedit from the start.
Quit without saving, safely
:q! throws away everything since the last write, with no confirmation. If you are not sure what you changed, check first from normal mode:
:w /tmp/backup-copy.txt
Save a copy somewhere harmless, then quit however you like. You can also inspect pending changes with :changes before deciding.
Recovering after a crash or dropped SSH session
Vim keeps a swap file (.filename.swp) next to the file while editing. If your connection drops mid edit, reopen the file and Vim offers recovery, or run it explicitly:
vim -r filename
After confirming your content is back, save it and delete the leftover swap file when prompted, or remove it manually if Vim keeps warning about it on every open.
Common errors recap
- E37: No write since last change: you typed
:qwith unsaved edits. Save with:wqor discard with:q!. - Commands appear IN the text: you typed
:wqwhile still in insert mode. PressEsc, delete the stray characters, then retry. - E212: Can't open file for writing: the directory does not exist or you lack permission; check the path and use the sudo tee trick above.
Saving and quitting with multiple files open
If you opened several files (vim a.conf b.conf) or split the window, the single file commands only close the current one. The whole session versions:
:wqa write every changed file and quit
:qa quit all, refusing if anything is unsaved
:qa! quit all and discard every change
Move between the opened files with :n (next) and :prev, and list them with :ls. A split window closes like any file with :q; only the last remaining window actually exits Vim.
If you would rather not deal with modes
nano ships on most distributions and shows its shortcuts on screen (Ctrl+O to save, Ctrl+X to exit). There is no shame in export EDITOR=nano, but knowing the three Vim commands at the top of this page will save you the day you land on a box where Vim is the only editor installed.