What is set hidden?
If you’ve ever browsed the .vimrc
of a Vim user (here’s an archive of some for perusal), you’ve most likely come across set hidden
. This setting is a fixture of many Vim configurations since Vim’s default configuration ships with set nohidden
, meaning hidden
is off.
But what does set hidden
even do and why would we want it? Shouldn’t we trust Vim’s defaults? Check :help hidden
for the official explanation, but it’s a bit obtuse. In a nutshell, by allowing hidden buffers with set hidden
, you’re telling Vim that you can have unsaved worked that’s not displayed on your screen.
Imagine opening a file with :e some.file
. You make some changed but you don’t write the file to disk with :w
. Then you open another file with :e some-other.file
. With the default set nohidden option, Vim will give you an error:
E37: No write since last change (add ! to override)
You can see an example of this in the video below. That’s Vim’s way of saying you can’t open a new buffer (and hide this one) without first writing it. If you want to allow this behavior, just add the set hidden
option to your .vimrc
. Many Vim users likely the flexibility that set hidden
offers, allowing them to move around files quickly without worrying about whether they’ve written to disk. It allows them to be deliberate about when and where they write files. Without hidden enabled, you may be required to write changes that you aren’t ready to write yet, just to see another file. If you have Syntastic checking enabled or some kind of background asset compilation that happens on write, maybe you don’t always want those triggering when you move around files. With set hidden
, you have that flexibility.
On the other hand, some users prefer the default set nohidden
, because it means they can’t lose work to a hidden buffer. Nothing is worse that than feeling like you’ve lost work when you’re staging changes for a commit. In reality, you made the change, just forgot to write the buffer. With set nohidden
, you have some guardrails that help you prevent unsaved work from getting backgrounded into the oblivion. But without hidden
on, you won’t be able to run commands on multiple files with Vim’s argdo
unless you also write those files in the same step with | w
.
So which do you prefer? Let us know with a comment, as we’d love to see what the prevailing sentiment is these days.