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

How do I switch between buffers without being forced to save first?

Answer

:set hidden

Explanation

By default, Vim refuses to let you switch away from a buffer that has unsaved changes, forcing you to save or discard with :w or :e! before moving on. Setting hidden tells Vim to allow unsaved buffers to exist in the background, so you can freely navigate between files without constant save prompts.

How it works

  • :set hidden enables the option globally
  • With hidden on, Vim keeps modified buffers in memory when you navigate away from them
  • You can switch buffers with :bnext, :bprev, :b {name}, <C-^>, or any other buffer command without saving first
  • Unsaved changes are preserved in memory — nothing is lost until you quit Vim
  • When you quit Vim with unsaved hidden buffers, Vim warns you and lists the modified buffers

Example

Without hidden, trying to switch from a modified buffer produces:

E37: No write since last change (add ! to override)

You are stuck until you :w or :e!. With hidden enabled, the switch happens silently and your unsaved changes wait in the background.

Add this to your vimrc:

set hidden

Now you can edit main.go, make changes, press :bnext to jump to utils.go, make changes there, press <C-^> to jump back to main.go — your unsaved edits in both files are intact.

Tips

  • This is considered essential by most experienced Vim users — many recommend it as the first line in any vimrc
  • Without hidden, commands like :argdo, :bufdo, and :cfdo fail partway through if any buffer has unsaved changes
  • When quitting, :qa will warn you about unsaved hidden buffers. Use :wa to save all modified buffers at once before quitting, or :qa! to discard everything
  • Use :ls to see which buffers have unsaved changes — they are marked with +
  • The hidden option does not auto-save anything — it simply allows unsaved buffers to exist in the background. You are still responsible for saving
  • Combine with set autowrite if you want Vim to automatically save the current buffer when switching away from it — this is an alternative philosophy to hidden
  • set hidden is required for many plugin workflows (like LSP rename-across-files) to function correctly

Next

How do I edit multiple lines at once using multiple cursors in Vim?