Vim Quit No Save
vim quit no save is the action of exiting Vim while discarding all unsaved changes, performed via :q! or ZQ from normal mode.
# Force quit without saving (discard all changes)
:q!
# Quit without saving and return non-zero exit code
:cq
# Quit without saving from normal mode (uppercase Z then uppercase Q)
ZQ
Tested on Ubuntu 22.04 with Vim 9.0.
Syntax
These commands work in command mode (press Esc to enter normal mode, then : to enter command-line mode). The ! overrides the “No write since last change” warning. ZQ works directly from normal mode without a colon.
Options and Flags (Colon-Command Variants)
| Command | Type | Default Behavior | Description |
|---|---|---|---|
:q! |
Force quit | Exits without saving | Overrides the “No write since last change” warning (E37). |
:cq |
Force quit with error | Exits without saving, returns exit code 1 | Useful in scripting to signal failure when file must not be saved. |
ZQ |
Normal-mode shortcut | Exits without saving | Works without entering command mode. Case-sensitive. |
:q |
Normal quit | Exits only if no unsaved changes | Fails with E37 if modifications exist. Use :q! to bypass. |
:qa! |
Force quit all buffers | Exits all open files without saving | Use when multiple files are opened with vim file1 file2. |
Usage Examples
1. Force quit after accidental modification
# Open a config file, make an unwanted change, then quit without saving
vim /etc/nginx/nginx.conf
# Press i to enter insert mode, accidentally type something, press Esc
:q!
Use this when you opened a file only to read it but accidentally triggered an edit. The exclamation mark forces Vim to exit ignoring the unsaved buffer.
2. Use :cq in a CI pipeline to abort on file corruption
# In a script, open a file for review, exit with error if unable to process
vim -c "/ERROR" -c "cq" logfile.txt
# If the pattern is found, Vim exits with code 1, causing the pipeline to fail
:cq returns exit code 1, allowing automated workflows to detect that the file was not saved and should be flagged.
3. Quit all open tabs and windows without saving
# When editing multiple files with vim -p file1 file2 file3, force exit all
:qa!
This variant closes every buffer, tab, and window unconditionally. Equivalent to running :q! on each buffer followed by quitting.
Troubleshooting and Common Errors
| Error Message | Root Cause | Resolution Command |
|---|---|---|
| E37: No write since last change (add ! to override) | Attempting :q with unsaved changes. |
:q! |
| E45: ‘readonly’ option is set (add ! to override) | File opened in read-only mode (vim -R or view). |
:q! – no need to save read-only files. |
| E89: No write since last change for buffer (add ! to override) | Using :q on a hidden buffer that is modified. |
:q! or :qa! to force quit all. |
| Still in Insert mode | Pressing : while in Insert mode inserts a colon instead of entering command mode. |
Press Esc first, then type :q!. |
Frequently Asked Questions
What is the difference between :q! and :cq in Vim?
Answer: :q! quits discarding changes with exit code 0; :cq quits discarding changes with exit code 1, signaling failure to scripts.
Use :q! for manual forced exit. :cq (cquit) is designed for CI/CD pipelines where a non-zero exit code is required to indicate a deliberate abort. Example:
:cq
When should I use :cq instead of :q! to quit without saving?
Answer: Use :cq in automated scripts or CI pipelines to intentionally exit with a failure status when discarding changes.
:q! exits with code 0 (success); :cq exits with code 1 (failure). This allows shell scripts to detect the abort. Example usage:
vim +:cq file.txt
How do I fix “E37: No write since last change” when trying to quit Vim without saving?
Answer: Use :q! to force quit without saving, or :cq to quit with exit code 1.
Vim’s :q command blocks exit if modifications exist. Append ! to override. :cq also forces exit but sets exit status to 1. Example:
:q!
Does :q! work on all platforms (Linux, macOS, Windows)?
Answer: Yes, :q! is a standard Vim ex command and works identically on Linux, macOS, and Windows with any Vim installation.
No platform-specific differences. Works in terminal Vim, gVim, and embedded versions. Verify with:
vim -c ":q!" -c ":qa"
What is the fastest way to quit Vim without saving any changes?
Answer: Press Esc, then type :q! and Enter (4 keypresses).
ZZ saves and quits – avoid. 😡 also saves. Fastest keystrokes for unsaved quit: Esc then :q! Enter. Map to a key for even faster:
:nnoremap <C-Q> :q!<CR>

Command Line Expert & Software Engineer
Welcome! I’m Thomas Heinrich, a software engineer and system administrator with a deep passion for the Command Line Interface (CLI). With years of experience navigating the terminal, building backend architectures, and automating server deployments, I created this space to share practical, real-world terminal knowledge.
Whether you are a beginner taking your first steps in a Linux environment or a seasoned DevOps engineer looking to optimize your deployment scripts, you will find actionable solutions here. My goal is to help you ditch the mouse, speed up your workflow, and harness the full power of the command line.