How to Save and Exit Vim: A Simple Guide
Learn the Vim commands to save, quit, discard edits, close multiple windows, and fix common errors such as E37.
If you are asking “How do I save and exit Vim?”, return to Normal mode, type :wq, and press Enter. Vim writes the file and closes the current window. If it is your last editing window, Vim exits.
The basic sequence is:
Esc
:wq
Enter
Esc makes sure you are in Normal mode. The colon opens Vim’s command line, and Enter runs the command.
How to save and exit Vim step by step
- Press
Escto leave Insert mode or another editing mode. - Type
:wq. You will see the command at the bottom of the screen. - Press
Enter.
:wq means “write, then quit.” The w writes the current buffer to its file, and q closes the current editing window. Vim documents this behavior in its editing reference.
Vim save and quit commands at a glance
| Goal | Command | Writes file? | Vim stays open? | Discard changes? |
|---|---|---|---|---|
| Save and keep editing | :w + Enter |
Yes | Yes | No |
| Quit an unchanged file | :q + Enter |
No | No | No |
| Save and quit | :wq + Enter |
Yes | No | No |
| Save only when modified, then quit | :x + Enter |
Only if changed | No | No |
| Save and quit shortcut | ZZ |
Only if changed | No | No |
| Quit and discard this buffer | :q! + Enter |
No | No | Yes |
| Quit all windows | :qa + Enter |
No | No | Only if none are modified |
| Write all files and quit | :wqa + Enter |
Yes | No | No |
| Discard all changes and quit | :qa! + Enter |
No | No | Yes, for every modified buffer |
Save without leaving Vim
Press Esc, type :w, and press Enter:
:w
Vim writes the current file and leaves you in the editor. Use this when you want to create a checkpoint before continuing to edit.
Quit Vim when nothing has changed
From Normal mode, run:
:q
This closes the current window if the buffer has no unsaved changes. If you changed the file, Vim refuses to quit so that you do not lose work.
How to quit Vim without saving
Press Esc, type :q!, and press Enter:
:q!
The exclamation mark forces Vim to abandon changes in the current buffer. Use it only when you are sure the edits should be discarded.
For every open window and buffer, use:
:qa!
Vim’s help documentation summarizes this as “Get out of Vim: Use ":qa!<Enter>"” and warns that all changes are lost. See the official Vim help for the versioned reference.
What is the Vim command to save and quit?
The standard command is :wq. Two useful alternatives are:
:xwrites only when the file changed, then quits.ZZdoes the same as:xfrom Normal mode, without a colon orEnter.
For ZZ, press uppercase Z twice:
Esc
ZZ
Do not type :ZZ; the shortcut is a Normal-mode key sequence.
Why Vim says “no write since last change”
If you run :q after editing, Vim may display:
E37: No write since last change (use ! to override)
This means the buffer has unsaved changes. Choose the action that matches your intent:
- Save and quit:
:wq - Save and keep working:
:w - Discard the edits:
:q!
Saving a new, unnamed buffer
A new buffer may not have a filename. In that case, :wq cannot know where to write it. Supply a path:
:w notes.txt
After the file has a name, save and quit normally:
:wq
You can also do both in one command:
:wq notes.txt
Read-only files and permission errors
If Vim reports that a file is read-only or cannot be written, :wq! may override Vim’s own read-only flag:
:wq!
The exclamation mark does not bypass operating-system permissions. If the account running Vim cannot write the file or directory, fix ownership or permissions, edit a copy, or reopen the file with the required privileges. A buffer with no name also cannot be written until you provide one.
Several files, tabs, or split windows
:q closes only the current window. When several windows are open:
:qaquits all windows, provided no modified buffer blocks the operation.:wqawrites changed buffers and quits all windows.:qa!quits everything and discards all unsaved changes.
Use :wqa when you want to preserve work in every open file. Use :qa! only after checking that losing every unsaved edit is acceptable.
Common mistakes and fixes
| Symptom | Cause | Fix |
|---|---|---|
Typing :wq inserts text into the file |
You are still in Insert mode | Press Esc, then type :wq and press Enter |
:q shows E37 |
The buffer has unsaved changes | Use :wq, :w, or :q! based on whether you want to save |
:wq says there is no file name |
The buffer is unnamed | Run :w filename, then :wq |
| Write fails with a permission or read-only error | The file or directory is not writable | Check operating-system permissions; ! alone is not a permission bypass |
ZZ does nothing expected |
You are not in Normal mode or pressed lowercase z |
Press Esc, then uppercase Z twice |
| Only one split closes | :q targets the current window |
Use :qa or :wqa for all windows |
A reliable decision checklist
- Press
Esc. - Decide whether the edits should be kept.
- If keeping them, use
:wqfor the current file or:wqafor all files. - If discarding them, use
:q!for the current buffer or:qa!for everything. - If Vim reports an error, read whether it concerns unsaved changes, a missing filename, or permissions, then apply the matching fix above.
Or skip the browser setup
ScreenshotNeo is a website screenshot API and MCP server, so a single request can capture a URL without configuring a browser. It accepts consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and each response reports the result in X-Page-Verdict and X-Billed headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients.
cURL:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
Python:
import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)
Node.js:
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
See the ScreenshotNeo documentation for the 63 capture options, including full-page and element shots, device presets, dark mode, custom CSS and JavaScript, waits, blocking rules, cookies, headers, geolocation, PDFs, caching, signed links, webhooks, bulk capture, and usage reporting. The free plan includes 1,000 screenshots each month with no card; paid plans start at $5 for 3,000. Create a free ScreenshotNeo account.
FAQ
What is the safest Vim command for beginners?
Use :wq after pressing Esc. It saves your edits and exits the current window.
Is :x better than :wq?
Both save and quit. :x avoids rewriting an unchanged file, while :wq explicitly writes it.
Can I undo a mistaken :q!?
Not from that Vim session. :q! discards unsaved buffer changes, so recover from a backup, version control, swap file, or another copy if one exists.
Why does Vim use a colon?
The colon switches from Normal mode to Command-line mode, where commands such as :w, :q, and :wq are entered.


