How to close buffer without closing the window?

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>

Or a command for your .vimrc (call with :Bd):

command Bd bp | sp | bn | bd

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.


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.


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.

Tags:

Vim