The three ways to close Vim
Vim closes in three ways: save and exit, exit without saving, or suspend the program and return to your terminal. The command you use depends on whether you want to keep the changes you made. If you are in insert mode (where you type text), you must press Escape first to return to normal mode before any of these commands will work.
The most common mistake is typing the command and nothing happens — this almost always means you are still in insert mode. Press Escape, then try again.
Key Takeaways
- Press Escape to leave insert mode, then type :wq to save your file and close Vim.
- Type :q! to close without saving if you made changes you do not want to keep.
- Type :q (without the exclamation mark) to close only if you have not made any unsaved changes.
- If Vim does not respond to your command, you are probably still in insert mode — press Escape and try again.
- Pressing Ctrl+Z suspends Vim and returns you to the terminal; type fg to bring it back.
Save and exit with :wq
The command :wq saves your file and closes Vim in one step. Type it exactly: colon, the letter w, the letter q, then press Enter. Vim will write the file to disk and return you to your terminal prompt.
This is the command you use when you have made changes and want to keep them. If the file did not exist before you opened Vim, :wq creates it. If the file already existed, :wq overwrites it with your new version.
Exit without saving with :q!
The command :q! closes Vim and throws away any changes you made since you opened the file. Type it exactly: colon, the letter q, an exclamation mark, then press Enter. The exclamation mark tells Vim you are sure you want to discard your work.
Without the exclamation mark, Vim will refuse to close if you have unsaved changes — it will show you an error message instead. The exclamation mark overrides that safety check. Use :q! only when you are certain you do not want to save.
Close without changes using :q
The command :q closes Vim only if you have not made any changes to the file since you opened it. Type colon and the letter q, then press Enter. If you have made changes, Vim will refuse and show an error message telling you to use :q! instead.
This command is a safety feature — it prevents you from accidentally closing Vim and losing work. If you see the error message, you have two choices: use :wq to save and close, or use :q! to close without saving.
Suspend Vim and return to the terminal
Pressing Ctrl+Z (hold Control, press Z) suspends Vim and returns you to your terminal prompt. Your file stays open in Vim's memory. To bring Vim back to the foreground, type fg at the terminal and press Enter.
This is useful when you need to run a terminal command without closing your file. You can suspend, run the command, then resume Vim exactly where you left off. If you suspend Vim and then close the terminal window, Vim will still be running in the background — you can reconnect to it later if you know how to use terminal sessions, but for most users it is safer to close Vim properly with :q or :wq.
What to do if nothing happens
If you type a command and Vim does not respond, you are almost certainly still in insert mode. In insert mode, Vim treats your keystrokes as text to type into the file, not as commands. Press the Escape key once, then type your command again.
You can tell you are in insert mode because the bottom left of the Vim window shows the word "INSERT". In normal mode, that area is empty. If you are unsure which mode you are in, press Escape — it does no harm if you are already in normal mode.
Frequently Asked Questions
What is the difference between :q and :q!?
:q closes Vim only if you have not made any changes. :q! closes Vim and discards any changes you made. The exclamation mark forces Vim to close even when you have unsaved work. Use :q! only when you are sure you do not want to save.
Can I undo changes after I close Vim with :q!?
No. Once you close Vim with :q!, the changes are gone and cannot be recovered. Vim does not keep a backup of unsaved changes. If you are unsure whether you want to save, use :wq instead — you can always delete the file later if you change your mind.
What does it mean when Vim says "No write since last change"?
This message means you have made changes to the file but have not saved them yet. Vim is warning you that if you close with :q, those changes will be lost. Use :wq to save and close, or :q! if you want to discard the changes.
Is there a way to close Vim without using commands?
No. Vim requires you to use commands to close it. You cannot close Vim by clicking a button or using a keyboard shortcut like Ctrl+W — those do not work in Vim. You must type the command, press Escape first if needed, and press Enter.
What happens if I close the terminal window while Vim is open?
Closing the terminal window will close Vim and discard any unsaved changes. Vim will not ask you to confirm or save first. Always close Vim properly with :wq or :q! before closing the terminal.