How To Quit Vim: A Step‑by‑Step Guide for Every User
Vim is a powerful, modal text editor loved by programmers, sysadmins, and writers alike. Its efficiency comes from a steep learning curve, and the most common stumbling block for beginners is learning how to quit Vim. This article walks you through the most reliable commands, shortcuts, and tips so you can exit Vim confidently—whether you want to save your changes, discard them, or simply close the editor without a fuss.
Why Exiting Vim Feels Tricky
Unlike graphical editors that offer a visible “X” button, Vim operates entirely in the terminal. Its modal nature means you must be in the correct mode before issuing a command. New users often stay in Insert mode and type :wq or :q! only to see “Not an editor command.” Understanding the difference between Normal, Insert, and Command‑line modes is the first step toward mastering how to quit Vim.
Basic Commands to Exit Vim
All exit commands start with a colon (:) which puts Vim into Command‑line mode. Here are the most common ways to leave:
- Save and quit: :wq or :x – writes the buffer to disk and closes Vim.
- Quit without saving: :q! – forces Vim to close, discarding any changes.
- Save only: :w – writes the file but keeps Vim open.
- Quit if no changes: :q – closes Vim only when the buffer is unchanged.
To use any of these, press Esc to ensure you are in Normal mode, then type the colon command and press Enter.
Quick Keyboard Shortcuts
If you prefer keystrokes over typing full commands, Vim offers several shortcuts that achieve the same result:
- ZZ – Save and quit (equivalent to :wq).
- ZQ – Quit without saving (equivalent to :q!).
These shortcuts must be entered in Normal mode. Simply press Esc, then type ZZ or ZQ quickly.
Handling Common Errors
Even after learning the commands, you might encounter messages like “E37: No write since last change (add ! to override).” This appears when you try to quit without saving changes. The fix is straightforward:
- Press Esc to return to Normal mode.
- Type :q! if you want to discard changes.
- Or type :wq to save