How to close buffer without closing the window?

24

11

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. :)

nperson325681

Posted 2011-05-27T06:34:52.840

Reputation: 1 401

Answers

13

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.

peth

Posted 2011-05-27T06:34:52.840

Reputation: 7 990

The script looks a solution I'm willing to accept if there is not official way. :) – nperson325681 – 2011-05-27T12:56:34.457

21

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.

Jonah Braun

Posted 2011-05-27T06:34:52.840

Reputation: 1 407

I was using vim-bbye but your answer is much more simpler and does not depend on any script. Thank you.

– Andrew-Dufresne – 2017-02-03T21:23:12.670

3

I lose my split if I close a buffer open in both windows, such as the last buffer. +1 for being simpler than the wikia scripts to bd without closing the window.

– Leif Carlsen – 2013-02-03T19:35:19.937

4

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.

garyjohn

Posted 2011-05-27T06:34:52.840

Reputation: 29 085

I was expecting that after a buffer delete, vim could switch to another buffer, or open a new one if necessary. – nperson325681 – 2011-05-27T07:06:07.670

@progo, that would require some scripting, i.e. plugin, or a simple mapping. – Clint Pachl – 2011-09-01T06:51:05.263

2

the bufkill.vim plugin works as well. I like to use it with vim-command-w for added functionality and niceties (like it will close a split if it's the last buffer or close vim if it's the last buffer/split).

Aaron Jensen

Posted 2011-05-27T06:34:52.840

Reputation: 141

2

Here's another solution:

map <C-W>o <C-W>n<C-W><C-W><C-W>c

Typing Ctrl+W then o will quietly create a new window and close the old window. The cursor is left in the new window. There are a number of positive effects:

  1. Your original split dimensions are preserved.
  2. An empty buffer is loaded in the new split.
  3. The original buffer is still loaded, use :buffers to list it.
  4. Ctrl+o moves the cursor to its original position in the old buffer should you need to go back.
  5. Works well even if only one window loaded.

rustushki

Posted 2011-05-27T06:34:52.840

Reputation: 21

1

Usually I just open the menu while on any buffer. This will open the menu on the left pane and cursor will be active there. So I just switch back to the active buffer and delete the buffer.

After that I can open other file from the menu.

My Steps is:

:Vex

ctrl + w + w

:bd

Apit John Ismail

Posted 2011-05-27T06:34:52.840

Reputation: 111

1

I think the problem is that most people expect vim buffer and window to be something they aren't.

People tend to think of a vim window as a standalone process that has its own list of buffers, but sadly it isn't. A vim window is only a viewport of its buffers.
Therefore a lot of problems arise, like your problem, or the problem that different window share the same buffer list so you can't edit Buffer 1 in Window A, and let Window B ignore Buffer 1 in its buffer list.

My solution is to open two instances of vim, so that you can have the kind of vim window you want. It works best on tiling WMs.

octref

Posted 2011-05-27T06:34:52.840

Reputation: 211