How to delete a buffer in Vim without losing the split window?

Vim

Vim Problem Overview


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?

Vim Solutions


Solution 1 - Vim

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.

Solution 2 - Vim

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

Solution 3 - Vim

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.

Solution 4 - Vim

Solution 5 - Vim

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.

Solution 6 - Vim

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

Solution 7 - Vim

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>

Solution 8 - Vim

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbyneriView Question on Stackoverflow
Solution 1 - VimMudView Answer on Stackoverflow
Solution 2 - VimGWWView Answer on Stackoverflow
Solution 3 - VimrrosaView Answer on Stackoverflow
Solution 4 - VimicecrimeView Answer on Stackoverflow
Solution 5 - VimlvarayutView Answer on Stackoverflow
Solution 6 - VimJeongpyo LeeView Answer on Stackoverflow
Solution 7 - VimDaftWoolyView Answer on Stackoverflow
Solution 8 - VimAlex MckayView Answer on Stackoverflow