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 with h in :ls but 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 hidden option is set, then :q behaves like :close by 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 :q loses all changes even when hidden is set. Therefore, if your intent is to keep a buffer’s changes, just prefer :close and 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.
    • :q quits Vim even if help window(s) are open.
    • :close won’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 as h in :ls.
    • 'hidden' option is ignored.
  • If you close the last non-help window with a modified buffer and 'hidden' is on.
    • :q fails with warning of unsaved changes.
    • :close fails 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 as h in :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 h in :ls.
    • The 'hidden' option is ignored.
  • 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 h in :ls and preserve changes.

: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 as h in :ls or quits Vim if it’s the last window.
    • 'hidden' option is ignored, but behavior is different from :q regardless of 'hidden'.
  • :close! fails if it’s the last window, else closes the window and preserves the changes to the buffer, marks buffer as h in :ls.
    • 'hidden' option is ignored, but the command behaves identically to :close if 'hidden' is on.

:hide

  • :hide behaves like :close but hides the buffer regardless of whether it is modified, marks buffer as h in :ls, and ignores 'hidden'.

  • It can be combined with another command. Useful with commands like :e that try to abandon the existing buffer but fail if it is modified and 'hidden' is off. :hide temporarily 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