3

I opened 5 buffers and in one of them I use the command :vs to split the current buffer screen into two.
When I use the command <leader>d to close one of the splitted buffers, the consequence is that the splitted window is also closed along with the buffer to be closed. I would like to close the buffer without impacting the split pattern of my screen.

For example, "buffer #1" and "buffer #2" are displayed in my current screen, I would like to close "buffer #1" with <leader>d and leave the screen to be a split of "buffer #3" and "buffer #2" instead of a not splitted screen with only "buffer #2" on it.

Is that requirement accessible?

  • Does :bnext in the window that you want to change buffers not solve this? What i mean is, what is missing from :bnext that a solution to your problem would have? – D. Ben Knoble Jul 22 '18 at 3:10
  • I need to close that buffer instead of switch to others. when use airline, the titles of the opened buffers always exists. And when use :bnext I will still switch to the that buffer that I would like to close. I need a method to close a buffer without impacting the split status of my screen. Hope I made my problem clear :) – coin cheung Jul 22 '18 at 3:51
  • Not sure what you mean by « close a buffer »—it can be hidden, deleted, or wiped out (as well as displayed in a window). Do you mean you want to delete the buffer and move to the next one? – D. Ben Knoble Jul 22 '18 at 3:58
  • yes, I would like to save the contents to file and delete the buffer, and I could recreate it if I need. I wish the place where the deleted buffer was displayed to be occupied with other buffers if any exists and I could still have that split screen. – coin cheung Jul 22 '18 at 5:35
  • Then you should look at :bd. – Lucas Jul 22 '18 at 9:50
3

vim-bbye does exactly what you're looking for. It provides :Bdelete and :Bwipeout, which are variants of :bdelete and :bwipeout that will delete the buffer but maintain the split and switch to another buffer, preserving your current split layout.

I personally mapped :Bdelete like so:

nnoremap <Leader>d :Bdelete<CR>

It's a terrific and simple plugin; I've been using it for over a year now.

  • Many thanks, that is very helpful. By the way, when I create a terminal in the last buffer :ter and then delete the buffer without close the terminal first, the buffer got removed but the terminal still exists as the last thing remained in the screen. Do you have some idea on how I could close the terminal when the last buffer is closed? – coin cheung Jul 23 '18 at 1:47
2

Try the following solution:

:nnoremap <Leader>d  :enew \| bd#<Return>

Limitation: you can not run this on unnamed buffer without affecting the split. If this is a concern, you may want to replace enew with e . etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.