The quickest way out: save and exit in one command
To save your changes and close Vim at the same time, type :wq and press Enter. The w stands for "write" (save), and q stands for "quit" (exit). This is the fastest route if you have made changes you want to keep.
If you have not made any changes to the file, you can simply type :q and press Enter to close Vim without saving. Vim will let you know if there are unsaved changes and refuse to close until you handle them.
Make sure you are in command mode before typing these commands. If you see text appearing on screen as you type, press Escape first to return to command mode, then type the command.
Key Takeaways
- Type :wq and press Enter to save your file and exit Vim in one step.
- Type :q to exit without saving if you have made no changes or do not want to keep them.
- If Vim refuses to close because of unsaved changes, use :q! to force exit without saving, though this will discard your work.
- Always press Escape first if you are in insert mode, so Vim recognizes your command instead of typing it as text.
- The colon at the start of these commands tells Vim you are entering a command, not editing the document.
What to do if Vim says the file has unsaved changes
When you try to quit with :q, Vim will show a message like "No write since last change" if you have made edits you have not saved. At this point you have three choices: save the changes, discard them, or go back to editing.
To save and exit, type :wq. To throw away your changes and close anyway, type :q! (the exclamation mark forces Vim to obey). To return to editing, press Escape and keep working — Vim will stay open.
The :q! command is permanent. Once you close the file without saving, those changes are gone. Use it only when you are certain you do not need the work.
Saving without exiting, if you want to keep working
If you want to save your changes but stay inside Vim and keep editing, type :w and press Enter. This writes the file to disk without closing the editor.
You can use :w as many times as you want while you are working. Many people save frequently this way to avoid losing work if something goes wrong. After you save with :w, you can continue typing and editing immediately.
How to tell if you are in the right mode to type a command
Vim has two main modes: insert mode (where you type text into the file) and command mode (where you type instructions to Vim itself). The commands :wq, :q, and :w only work in command mode.
If you are in insert mode, any text you type goes into the file instead of being read as a command. You will see the word "INSERT" at the bottom left of the screen. Press Escape to switch to command mode, and the "INSERT" label will disappear.
Once you are in command mode, you can type the colon (:) followed by your command. The colon tells Vim that what comes next is an instruction, not text for the document.
Other ways to save if the standard commands do not work
If :wq does not seem to be working, make sure you pressed Escape first and that you are actually in command mode. Type the command slowly and check that you have the colon at the start. Then press Enter — the command will not run until you do.
Some systems or Vim configurations may use different key combinations, but :wq is the standard across almost all versions of Vim. If you are using a different editor that looks like Vim (such as Vi or Neovim), the same commands usually work.
If you have opened a file you do not have permission to edit, Vim will refuse to save it. The error message will say something like "Permission denied". In that case, you can save to a different filename with :w newfilename, or you may need to close the file and open it with higher permissions.
What happens if you close the terminal without saving
If you close the terminal window or lose your connection while Vim is still open, your unsaved changes will be lost. Vim does create a recovery file (a hidden backup) in some cases, and it may offer to recover your work the next time you open that file.
This recovery feature is not may provide, so it is better to save regularly with :w than to rely on it. If you have made important changes, always use :wq to save and exit properly before closing the terminal.
Frequently Asked Questions
What is the difference between :wq and :x?
Both :wq and :x save and exit, but :x only writes the file if you have actually changed it. If you opened a file and made no edits, :x closes it without updating the file's timestamp. For most purposes, :wq is simpler and more predictable.
Can I undo changes after I close Vim?
No. Once you save with :wq or :w, the changes are written to disk and undo is no longer possible inside Vim. If you saved changes you did not mean to keep, you will need to edit the file again to fix them. This is why :q! exists — to close without saving if you realize you made a mistake.
What if I type :wq but nothing happens?
Make sure you pressed Enter after typing the command. Vim will not run the command until you do. Also check that you are in command mode (press Escape first if you see "INSERT" at the bottom). If you typed the colon and command correctly and pressed Enter, the file should save and close within a second or two.
Is there a way to save multiple files at once in Vim?
If you have multiple files open in Vim (using tabs or split windows), :wq saves and closes only the current file. To save all open files without closing, type :wa. To save all and exit completely, type :wqa.
What does the exclamation mark do in :q!?
The exclamation mark forces Vim to obey the command even if there are unsaved changes. Without it, Vim refuses to close and shows an error. With :q!, Vim closes immediately and discards any work you have not saved, so use it only when you are certain.