vimtricks.wiki Concise Vim tricks, one at a time.

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 proceed
  • N — discard changes and proceed
  • C — cancel and return to editing

Scope

  • :set confirm — apply globally for the session
  • :setlocal confirm — apply only to the current buffer
  • Add to ~/.vimrc to 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

  • confirm is a softer alternative to set autowrite (which silently saves on every buffer switch)
  • Combine with :set hidden to allow navigating away from modified buffers without any prompt — then rely on confirm only at actual quit time
  • :help 'confirm' explains all the command interactions

Next

How do I run a search and replace only within a visually selected region?