Vim: A Comparison of :quit :close :hide
The finer differences between Vim’s :quit and
:close can be hard to digest, so let’s
start with a high level comparison.
-
:clo[se[!]]closes the current window, hides its buffer, which usually means flagging it withhin:lsbut not always, and preserves any changes. However, it never closes the last window (counting help windows), never closes Vim, and never abandons changes to the buffer. -
:q[uit[!]]closes the current window, (usually) abandons changes to the buffer, and quits Vim if it’s the last window, not counting help windows. -
:q!never preserves changes to the buffer,:close!always does. -
If the current window is not the last window and the
hiddenoption is set, then:qbehaves like:closeby closing the window, hiding the buffer and preserving changes. This is an exception to:q’s usual behavior and might surprise you. For instance, if the window is the last then:qloses all changes even whenhiddenis set. Therefore, if your intent is to keep a buffer’s changes, just prefer:closeand you won’t have to worry about it.
From this high-level view, we see that these
commands are usually quite different. Let’s take
a closer look. Also, if you are not comfortable
with the 'hidden' option, use :set hidden? to
examine its current value, :set hidden to turn
it on, and :set nohidden to turn it off.
:close and :q
:close and :q behave differently in the
following cases:
- If you close the last non-help window with an
unmodified buffer.
:qquits Vim even if help window(s) are open.:closewon’t quit Vim. The command fails if there are no help windows. If there are, command succeeds, leaving those help windows open; changes are preserved and the buffer is marked ashin:ls.'hidden'option is ignored.
- If you close the last non-help window with a
modified buffer and
'hidden'is on.:qfails with warning of unsaved changes.:closefails if there are no help windows (‘Can’t close last window.’). If there are help windows, command succeeds, leaving those help windows open; changes are preserved and the buffer is flagged ashin:ls.
:close and :q behave the same way in the following
remaining cases:
- If you close a window with a modified buffer and
'hidden'is off.- Both commands fail with warning that the buffer contains unsaved changes.
- It doesn’t matter if it’s the last window.
- If you close a window with an unmodified buffer that
is not the last window.
- Both commands close the window and hide the buffer
but the buffer is not flagged with
hin:ls. - The
'hidden'option is ignored.
- Both commands close the window and hide the buffer
but the buffer is not flagged with
- If you close a window that is not the last
(not counting help windows), it has a modified buffer
and
'hidden'is on.- Both commands close the window, flag the buffer
with
hin:lsand preserve changes.
- Both commands close the window, flag the buffer
with
:close! and :q!
:close! and :q! behave differently when the buffer
has been modified or if it is the last window.
:q!closes the window and abandons changes to the buffer even if'hidden'is on, marks buffer ashin:lsor quits Vim if it’s the last window.'hidden'option is ignored, but behavior is different from:qregardless of'hidden'.
:close!fails if it’s the last window, else closes the window and preserves the changes to the buffer, marks buffer ashin:ls.'hidden'option is ignored, but the command behaves identically to:closeif'hidden'is on.
:hide
-
:hidebehaves like:closebut hides the buffer regardless of whether it is modified, marks buffer ashin:ls, and ignores'hidden'. -
It can be combined with another command. Useful with commands like
:ethat try to abandon the existing buffer but fail if it is modified and'hidden'is off.:hidetemporarily sets'hidden'on when the other command runs, then sets it back to its previous value. Example from the Vim Online Help::hide edit Makefile