99

Often, I'll load some information into a temporary buffer, and then close it with :clo!. But this hides the buffer rather than unloading it entirely, even though I have nohidden set. I find this tiresome since I get an extra confirmation when I quit Vim, about content I already (thought I) discarded.

I could just use :q! instead, but that's a bad habit to get into because it will exit Vim entirely if there's only one buffer left, which is also annoying. Worse, if I typo :qa!, I could lose a lot of work.

Is there a middle ground? I'd really like some command which kills the current buffer but doesn't exit Vim. I'm not opposed to writing it in Vimscript, but I'd rather avoid it if the command already exists.

2 Answers 2

117

There is command to do exactly that: :bdelete or just :bd. By default it will unload current buffer. To unload other buffer, first get the list of all buffers with :buffers command, and after that you can specify the number after :bd to remove it. Also :bd + space + tab allows completion using buffer name.

6
  • 1
    This will operate on the last open buffer, which isn't exactly what I wanted, but since it doesn't quit, it's good enough for my purposes.
    – Kevin
    Apr 25, 2015 at 5:23
  • 1
    The vim-bbye plugin might be what you're looking for: github.com/moll/vim-bbye
    – alxndr
    Apr 25, 2015 at 17:08
  • 1
    This does not work in vi
    – redolent
    Aug 16, 2017 at 13:36
  • Also, the built-in alias :ls is easier (imo) to remember and does the same as :buffers
    – kuzyn
    Oct 6, 2017 at 10:52
  • 2
    This doesn't unload the buffer. It actually doesn't even close it. It simply unsets 'buflisted'. vim.wikia.com/wiki/Vim_buffer_FAQ
    – BTRUE
    Feb 16, 2018 at 7:32
48

You can completely wipe out a buffer using the :bwipeout (or :bw) command. This completely removes the buffer from memory, including any marks, option settings, etc. that you have added to it. Similarly, :bdelete (or :bd) removes the buffer, but leaves it in memory and keeps marks and option settings.

As per the comment by Tom Hale, the Vim documentation recommends using :bd over :bw unless you know what you are doing. I tend to use :bw because like the idea of completely removing the buffer from memory, and I don't make much use of marks, buffer-specific option settings, etc, to the point of needing them to remain after closing my buffer.

Like the :quit (:q) command, Vim will give an error if the buffer has changed. To address this, you can append an exclamation point after the command to suppress the prompt. Another option, instead of adding exclamation points to everything, is to add "set confirm" to your vimrc. With this set, vim will prompt you to save file changes on close.

:bufdo is a useful command that performs another command on all active buffers. Combining the :bufdo command with the :bw/:bd command lets you remove all active buffers at once. You can still use the exclamation point to suppress errors, but whether you place it after :bufdo, after :bw, or after both causes different results for each:

:bd          - deletes the current buffer, error if there are unwritten changes
:bd!         - deletes the current buffer, no error if unwritten changes
:bufdo bd    - deletes all buffers, stops at first error (unwritten changes)
:bufdo! bd   - deletes all buffers except those with unwritten changes
:bufdo! bd!  - deletes all buffers, no error on any unwritten changes

:bw          - completely deletes the current buffer, error if there are unwritten changes
:bw!         - completely deletes the current buffer, no error if unwritten changes
:bufdo bw    - completely deletes all buffers, stops at first error (unwritten changes)
:bufdo! bw   - completely deletes all buffers except those with unwritten changes
:bufdo! bw!  - completely deletes all buffers, no error on any unwritten changes

:set confirm - confirm changes (Yes, No, Cancel) instead of error

Here are a few other useful buffer commands:

:ls          - list open buffers
:b N         - open buffer number N (as shown in ls)
:tabe +Nbuf  - open buffer number N in new tab
:bnext       - go to the next buffer (:bn also)
:bprevious   - go to the previous buffer (:bp also)

There is a lot more to buffer handling that is out of the scope of this question. Have a look at:
http://vim.wikia.com/wiki/Vim_buffer_FAQ http://vimdoc.sourceforge.net/htmldoc/windows.html#buffers

7
  • Can you please add some explanation to this answer? Nov 5, 2015 at 0:56
  • The help says "do no use this unless you know what you are doing", indicating that :bd is preferred in most cases
    – Tom Hale
    Jul 29, 2017 at 9:07
  • 2
    @TomHale It'd be nice to know what else :bw does beyond "Everything related to the buffer is lost. All marks in this buffer become invalid, option settings are lost, etc.". That seems perfectly reasonable to me if I want to to 'close' a buffer like it would be if I quit Vim entirely. Jul 16, 2018 at 20:52
  • 2
    I prefer :bw over :bd because if you use :bd and then press <ctrl-^> to go to the last used buffer, it will bring it up from the memory. :bw gives me more control and does what I want ^^
    – MacMartin
    Sep 5, 2018 at 10:00
  • 1
    I love that :bd just unlists the buffer so I can retrieve it later. Sometimes that's what you want. But I also use :bw with abandon. When I've written changes and finished with the file I don't want it bugging me in the :buffers! list either. The dire warnings in help is just to protect noobs from spoiling their process. It's not like :bw could even lose you writes, unless you use :bw!, but if you want to wipe a scratch file or something, don't sweat it, just wipe it! It's not that hard!
    – NeilG
    Mar 18, 2020 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.