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 hiddenenables the option globally- With
hiddenon, 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:cfdofail partway through if any buffer has unsaved changes - When quitting,
:qawill warn you about unsaved hidden buffers. Use:wato save all modified buffers at once before quitting, or:qa!to discard everything - Use
:lsto see which buffers have unsaved changes — they are marked with+ - The
hiddenoption does not auto-save anything — it simply allows unsaved buffers to exist in the background. You are still responsible for saving - Combine with
set autowriteif you want Vim to automatically save the current buffer when switching away from it — this is an alternative philosophy tohidden set hiddenis required for many plugin workflows (like LSP rename-across-files) to function correctly