How do I save only real file buffers when running bufdo across many open buffers?
Answer
:bufdo if &buftype ==# '' | update | endif
Explanation
bufdo is powerful for multi-buffer automation, but a naive write pass can error on special buffers (help, terminal, quickfix, prompts). Guarding with &buftype lets you target real file buffers only, then update writes just those that changed. This pattern keeps large sessions clean without noisy failures or unnecessary writes.
How it works
:bufdoiterates through every listed buffer.if &buftype ==# ''keeps execution on normal file-backed buffers only.updatewrites the buffer only when modified.endifcloses the conditional per buffer iteration.
This is safer than blanket :wall in mixed workflows where docs, quickfix lists, terminals, and scratch buffers coexist.
Example
Session contains:
1. app/main.go (modified)
2. :help window (special buffer)
3. terminal job (special buffer)
4. app/config.yaml (unmodified)
Run:
:bufdo if &buftype ==# '' | update | endif
Result:
- app/main.go is written
- app/config.yaml is skipped (already clean)
- help/terminal buffers are ignored
Tips
- Pair with
:silentif you want minimal command-line noise. - For substitutions plus save in one sweep, chain commands:
:bufdo %s/foo/bar/ge | if &buftype ==# '' | update | endif. - If you only want loaded buffers, combine with
:lsreview before running.