I usually open a few windows and keep some buffers open. Since my MO in buffer exploring is to use quick shortcuts to :bn and :bp, I want to close unneeded buffers from distracting my buffer surfing.

But the pain is, issuing :bd and :bw results in closing the window as well, in case I have multiple ones open. How do I close (delete) a buffer and leave the windows intact?

Solution inspired by @peth's answer

:command! BW :bn|:bd#

It is simple. Doesn't work well with only one buffer open (I get different behaviour depending on the way I open the files) but it isn't a big issue. :)

link|improve this question

78% accept rate
feedback

3 Answers

up vote 4 down vote accepted

It can be a pain, this question is raised over and over on #vim (Freenode), too.

Some people recommend not deleting buffers and switching with :b partial<Tab> instead of :bn, which works, but isn't what you're asking.

The best way I know of to do that is to switch to another buffer and then :bd#, which you could map, of course.

See also Deleting a buffer without closing the window on the Vim wikia.

link|improve this answer
The script looks a solution I'm willing to accept if there is not official way. :) – progo May 27 '11 at 12:56
feedback

A window is a viewport into a buffer. (See :help window.) You can't have a window without an associated buffer. You can use a command such as :enew to replace the current window contents with an empty buffer, though.

link|improve this answer
I was expecting that after a buffer delete, vim could switch to another buffer, or open a new one if necessary. – progo May 27 '11 at 7:06
@progo, that would require some scripting, i.e. plugin, or a simple mapping. – Clint Pachl Sep 1 '11 at 6:51
feedback

I messed with this a bit and finally came up with:

:bp | sp | bn | bd

Here's the copy/paste version for key mapping:

:bp<bar>sp<bar>bn<bar>bd<CR>

I've tested it a fair bit and it works consistently in various conditions. When used on the last buffer it will leave you with a new blank buffer.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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