How do I make Vim ask me to confirm instead of failing when I try to close an unsaved file?
Answer
:set confirm
Explanation
With :set confirm (or set confirm in your vimrc), Vim prompts you with a y/n/a/q dialog whenever a command would fail due to unsaved changes — such as :q, :e {file}, :bn, or :wqa. Instead of getting an error message and stopping, Vim asks what you want to do.
Without confirm
:q
E37: No write since last change (add ! to override)
You must either :w first, or force with :q! (discarding changes).
With confirm
:q
Save changes to "myfile.py"?
[Y]es, (N)o, (C)ancel: _
You can choose:
Y— save and proceedN— discard changes and proceedC— cancel and return to editing
Scope
:set confirm— apply globally for the session:setlocal confirm— apply only to the current buffer- Add to
~/.vimrcto make it permanent:set confirm
When it applies
confirm triggers for:
:q,:qa,:wqa— quit commands:e {file},:bn,:bp— switching buffers with unsaved changes:bd— deleting a modified buffer
Tips
confirmis a softer alternative toset autowrite(which silently saves on every buffer switch)- Combine with
:set hiddento allow navigating away from modified buffers without any prompt — then rely onconfirmonly at actual quit time :help 'confirm'explains all the command interactions