Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

People also ask

How do I delete a buffer in Vim?

Assuming the default backslash leader key, you can also press \bd to close (delete) the buffer in the current window (same as :Bclose ).

How do Vim buffers work?

A buffer is the in-memory text of a file. Any time we open an existing file or create a new one using Vim, a buffer will be allocated as the in-memory representation of said file. Any changes we make will be tracked within the buffer. When we are ready to update the file, the buffer can be written to disk.


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.


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


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.


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


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.


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