How can I close a buffer without closing the window?

Viewed 33317

Vim's multilayered views (Windows, Buffers and Tabs) left me a little confused. Let's say I split the display (:sp) and then select a different buffer to display in each window. Now I want to close one of the buffers, yet I don't want the window to close (After the closing it can display the next buffer on the list or an empty buffer, it doesn't matter). How can I do this?

17 Answers

A simple version I use personally is

:bp|bd#

It goes to the previous buffer and deletes the other buffer (which is actually the original where we jumped from). This does what you would expect in 99% of cases without any complicated scripts.

As a keyboard shortcut I use the following

nnoremap <silent> <Leader>c :bp<BAR>bd#<CR>

Simply do :new|bd# or Paste this into your vimrc

let mapleader = " "
" CLOSE current Buffer without closing window
nnoremap <silent><leader>d :new<BAR>bd#<CR>
" CLOSE current window
noremap <leader>x <c-w>c

Then hit (space + d) or (space + x)

EDIT: even better with

nnoremap <silent><leader>d :new<BAR>bd#<BAR>bp<CR>

I like this answer most, although I prefer

<CTRL-^>:bd#

because it is faster to type and I don't need a keymapping.

The command <CTRL-^> (same as on English keyboard) switches to the alternate file and :bd# deletes the other buffer.

If you're like me and you came here trying to do the opposite, close the window without closing the buffer, you can do that via:

:close

Related