176

When a buffer gets deleted (via the :bd[elete] command), it not only deletes the buffer but also removes the split window that buffer was in.

Is there a way to delete/unload a buffer and keep the window split?

ib.
  • 27,830
  • 11
  • 80
  • 100
byneri
  • 4,050
  • 5
  • 27
  • 28
  • 3
    possible duplicate of [how to remove a buffer from gvim without closing the window](http://stackoverflow.com/questions/4403593/how-to-remove-a-buffer-from-gvim-without-closing-the-window) – ZyX Dec 16 '10 at 20:58
  • [This one `DeleteCurBufferNotCloseWindow()` works well and is quite readable] (https://stackoverflow.com/a/44950143/3625404). – qeatzy Jul 06 '17 at 13:41
  • [This](http://www.vim.org/scripts/script.php?script_id=2103) worked well for me. – Rumple Stiltskin Dec 17 '10 at 03:54
  • 2
    Possible duplicate of [Vim close buffer but not split window](https://stackoverflow.com/questions/4298910/vim-close-buffer-but-not-split-window) – ezdazuzena Nov 29 '17 at 08:47
  • a detailed answer is here -> https://vi.stackexchange.com/a/5350/31545 – STEEL Sep 23 '20 at 15:26

8 Answers8

381

bp|bd # will do it.


Details: The bp command (“buffer previous”) moves us to a different buffer in the current window (bn would work, too), then bd # (“buffer delete” “alternate file”) deletes the buffer we just moved away from. See :help bp, :help bd, and :help alternate-file.

ib.
  • 27,830
  • 11
  • 80
  • 100
Mud
  • 28,277
  • 11
  • 59
  • 92
  • 80
    You have no idea how long I've been looking for something like this which is so simple, not requiring and entire script to run it. In my VIMRC, I now have this mapped for CTRL+C: `nnoremap :bp\|bd #` – Cloud Jul 12 '13 at 17:40
  • 12
    I'm trying to remap using `nmap d :bp|bd #`, but I'm getting `E94: No matching buffer for #`. – Fábio Perez Dec 10 '13 at 01:50
  • 27
    Put a backslash in front of the `|` (e.g. `:bp\|bd`) – Mud Dec 10 '13 at 20:04
  • 5
    I knew I shouldn't have to install a plugin for this. Thanks! – Akash Aug 20 '14 at 08:59
  • 2
    `b#|bd #` will switch to the last used buffer instead of the next/previous. However, it will jump to hidden buffers if you have `set hidden`, so I made a more intelligent BD function to avoid that. – c24w Nov 07 '14 at 16:59
  • 1
    Nice and simple, though this doesn't destroy the last buffer if you're on it. – Derek Morrison Nov 15 '14 at 07:19
  • 2
    Sad that i can't hit the upvote button multiple times. Awesome solution. Thanks! – danbruegge Feb 13 '15 at 05:42
  • It's times like this I wish I could give more than one upvote. Not sure why it took me ~5 years to find this answer that's been here the whole time, but I'm so glad I finally found it. – redbmk Jun 28 '16 at 12:51
  • @DerekMorrison that was also annoying me, if you want to close the last buffer too you can use `nmap len(filter(range(1, bufnr('$')), 'buflisted(v:val)')) == 1 ? ':bd' : ':bp:bd #'` not pretty, but it works :) – supercrabtree Dec 21 '16 at 06:12
  • 5
    Not perfect because this will close windows if the buffer is open on several of these, otherwise works very well. A perfect solution would replace the buffer in all windows where it is open, and if no buffer was left it would open a "scratch" buffer on the windows. The perfect solution would never ever close any windows. – mljrg Nov 21 '18 at 19:53
28

I really like bufkill.vim there is a github repo as well

GWW
  • 43,129
  • 11
  • 115
  • 108
  • 4
    Thanks a lot, this vim behavior has bugged me a lot! btw, latest version of bufkill is at https://github.com/qpkorr/vim-bufkill – fsrechia Jan 12 '17 at 12:06
  • 1
    I used [bufkill](https://github.com/qpkorr/vim-bufkill) for years but found its autocmds to maintain a buffer list often threw errors. I evaluated some other plugins that accomplish a similar goal. I preferred the simplicity of [vim-bbye](https://github.com/moll/vim-bbye). The other alternative was [vim-sayonara](https://github.com/mhinz/vim-sayonara) which tries to replace all buffer/window closing commands and Do The Right Thing, but I prefer more manual control. – idbrii Apr 12 '21 at 17:42
19

You can add the following to your .vimrc to have Bd work as bd but without touching the window splits:

command Bd bp\|bd \#

I found this as a useful complement to what Mud answered.

Community
  • 1
  • 1
rrosa
  • 368
  • 4
  • 9
  • That's exactly what I have in my .vimrc. :) – Mud Jun 29 '15 at 16:12
  • And you can add the 3 commands for unloading, deleting and wiping out a buffer: command! BUN bp\|bun \# command! BD bp\|bd \# command! BW bp\|bw \# which is equivalent to what bufkill provide but with 3 lines in vimrc instead of a 700+ lines plugin ! – fievel Jul 12 '18 at 07:48
  • 4
    For me it worked only with this in .vimrc `command! Bd bp|bd #`. The command above with the `\` kept the buffer in the buffers list. – mljrg Nov 22 '18 at 10:47
13

See deleting a buffer without closing the window on VIM tips wiki.

icecrime
  • 74,451
  • 13
  • 99
  • 111
3

I do something similar to @Mud, but switch to previous view buffer, #, instead of the previous buffer in buffer list. Here is a binding key in my .vimrc:

nnoremap <silent> <leader>q :lclose<bar>b#<bar>bd #<CR>

Close Location windows, if exist, switch to the previous view buffer, and then close the last switched buffer.

lvarayut
  • 13,963
  • 17
  • 63
  • 87
1

My Choice is

:sb # | bd #
:sb 1 | bd #
: <1. Recall Buffer> | <2. Delete Buffer>

Think Like that! /// <1. Recall Buffer> | <2. Delete Buffer>

:vert sb 2 | bd #
:vert sb <tab key~completed file(buffer)name> | bd #

why?! It's easy to remember 3 (+ 1) keyword!

  1. sb split_buffer
  2. bd delete buffer ▶ simple 2 keywords
  3. # or Number of buffer
  4. vert ▶ short_form of vertical (split_buffer or else)

That are easy and very useful in many other many case!

Have a nice Day! :)

0

I used to use :

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

But I found certain occasions where it closed my window. On top of that the next or previous buffer might not be what you want to be displayed in the split.

Now I do this :

  • switch to the buffer I want to work on
  • Delete the alternate buffer

nnoremap <leader>d :bd#<CR>

DaftWooly
  • 348
  • 3
  • 7
-1

I actually like the behaviour of :bd some of the time so I use the following combination:

nmap <silent> <Leader>d  :enew \| bd#<Return>
nmap <silent> <Leader>w :bd<CR>

This allows me to use <Leader>d to close a buffer whilst maintaining splits and <Leader>w to close a buffer and split simultaneously.

  1. :enew creates a new buffer and switches focus to it
  2. bd # delete the last buffer that was focused
Alex Mckay
  • 3,463
  • 16
  • 27