How can I close a buffer without closing the window?

Vim

Vim Problem Overview


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?

Thanks.

Vim Solutions


Solution 1 - Vim

I messed with this a bit and finally came up with:

:bp | sp | bn | bd

Here's the copy/paste version for key mapping:

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

I've tested it a fair bit and it works consistently in various conditions. When used on the last buffer it will leave you with a new blank buffer.

Throw this in your .vimrc:

map <leader>q :bp<bar>sp<bar>bn<bar>bd<CR>

Restart Vim, or just :source ~/.vimrc for changes to take effect. Next time you want to close a buffer just type: \q (if \ is your leader key)

Solution 2 - Vim

I searched for this today and came up with

:b#|bd#

which changes the current window to the previously open buffer and deletes/hides the buffer you just switched away from.

This requires at least two known buffers.

If another window but the current shows the same buffer this will still destroy splitting. You can change all windows to the previously open buffer with

:windo b#

I added more detail about the former command discussing a mapping for it (and some pitfalls) in an answer to a similar question.

Solution 3 - Vim

There's a script on the Vim wiki to do this. I don't think there is a builtin that does what you want.

The latest version of vim-bufkill is on github.

Solution 4 - Vim

nmap <leader>d :bprevious<CR>:bdelete #<CR>

Works as it should until one buffer is open in several windows. Good enough unless you want to use the bigger scripts out there.

Edit: this is what i use right now:

function! BufferDelete()
	if &modified
		echohl ErrorMsg
		echomsg "No write since last change. Not closing buffer."
		echohl NONE
	else
		let s:total_nr_buffers = len(filter(range(1, bufnr('$')), 'buflisted(v:val)'))

		if s:total_nr_buffers == 1
			bdelete
			echo "Buffer deleted. Created new buffer."
		else
			bprevious
			bdelete #
			echo "Buffer deleted."
		endif
	endif
endfunction

Solution 5 - Vim

I think this is what you're looking for

http://www.vim.org/htmldoc/windows.html#window-moving

Try this:

Look ar your buffer id using

:buffers

you will see list of buffers there like

1  a.cpp
2  b.py
3  c.php

if you want to remove b.py from buffer

:2bw

if you want to remove/close all from buffers

:1,3bw

Solution 6 - Vim

For those who use NERDTree.

I fix this using this plugin https://github.com/jistr/vim-nerdtree-tabs and now I can close the only buff/file/tab without closing the window.

After having the plugin above installed put the following code on my .vimrc:

let g:nerdtree_tabs_autoclose=0

The description for the variable above is: Close current tab if there is only one window in it and it's NERDTree (default 1)

More info here: https://github.com/jistr/vim-nerdtree-tabs#configuration

Solution 7 - Vim

I don't think there is a one shot way to do this, but you could do :enew or :ls to list your buffers and swap to a different one using :b [number].

Once you've got a different buffer in the window :bd # will delete the previous buffer in the window, and since the current buffer still exists the window won't be closed.

Solution 8 - Vim

I used to use :

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

But I found certain occasions where it closed my window.

Recently I noticed that I always use this when I am working on a project and need to quickly open my .tmux.conf .zshrc before going back to work.

For this usage, I find better to :

  • switch back to the buffer I was previously working on with C-6
  • type :bd# to delete the previous buffer (I have mapped it like this : nnoremap <leader>d :bd#<CR>)

It allows me to control the buffer I'm going back to and feels more natural.

Solution 9 - Vim

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>

Solution 10 - Vim

Would

:enew

do what you want? it will edit a new, unnamed buffer in the current window leaving the existing file open in any other windows.

Solution 11 - Vim

My favorite solution for this is the bufkill.vim plugin (GitHub). It provides alternate versions of the various :b-prefix commands that work the same as their counterparts, but leave the window open. They display whatever the last visible buffer contained, or an empty/new buffer if there was no prior buffer.

From the documentation:

>

When you want to unload/delete/wipe a buffer, use:
>   :bun/:bd/:bw to close the window as well (vim command), or
>   :BUN/:BD/:BW to leave the window(s) intact (this script).

Solution 12 - Vim

To 'close' a view, use :hid[e]. Works if you have managed to split the viewport or opened multiple files. You can't hide the last buffer on display.

1 Further tip that helped me: use :e ./path/to/file.work to open a file in viewport without splitting the window.

P.S. At two days into vim I still have trouble finding the precise help commands. Hopefully this will help someone else keep working until they really get time to understand vim.

Solution 13 - Vim

Here is a very readable vimscript function, which handles all cases well,

  • behave similar to built-in:bd (if only one window, just invoke it!),
  • issue a warning and do nothing if buffer modifed.
  • if no other buffer, create one, via :enew.
  • if alternate buffer exist and in buffer-list, switch to it, else go next, via:bn.
  • more reasonable behavior for multiple-window layout
  • not closing any window,
  • always stay on the original window.
  • for each window that displays current buffer, do as listed above, then delete old current buffer.

nnoremap <Leader>b :call DeleteCurBufferNotCloseWindow()<CR>

func! DeleteCurBufferNotCloseWindow() abort if &modified echohl ErrorMsg echom "E89: no write since last change" echohl None elseif winnr('$') == 1 bd else " multiple window let oldbuf = bufnr('%') let oldwin = winnr() while 1 " all windows that display oldbuf will remain open if buflisted(bufnr('#')) b# else bn let curbuf = bufnr('%') if curbuf == oldbuf enew " oldbuf is the only buffer, create one endif endif let win = bufwinnr(oldbuf) if win == -1 break else " there are other window that display oldbuf exec win 'wincmd w' endif endwhile " delete oldbuf and restore window to oldwin exec oldbuf 'bd' exec oldwin 'wincmd w' endif endfunc

Solution 14 - Vim

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>

Solution 15 - Vim

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.

Solution 16 - Vim

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

Solution 17 - Vim

use ":bd" as a command.

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
QuestionMoshView Question on Stackoverflow
Solution 1 - VimJonah BraunView Answer on Stackoverflow
Solution 2 - VimvalidView Answer on Stackoverflow
Solution 3 - VimBrian CarperView Answer on Stackoverflow
Solution 4 - VimggustafssonView Answer on Stackoverflow
Solution 5 - VimNopeView Answer on Stackoverflow
Solution 6 - VimLucas SerafimView Answer on Stackoverflow
Solution 7 - VimAndrew KhosravianView Answer on Stackoverflow
Solution 8 - VimDaftWoolyView Answer on Stackoverflow
Solution 9 - VimDorian B.View Answer on Stackoverflow
Solution 10 - VimmikejView Answer on Stackoverflow
Solution 11 - VimJim StewartView Answer on Stackoverflow
Solution 12 - VimSennekuylView Answer on Stackoverflow
Solution 13 - VimqeatzyView Answer on Stackoverflow
Solution 14 - VimDoftomView Answer on Stackoverflow
Solution 15 - Vimd2weberView Answer on Stackoverflow
Solution 16 - VimxdhmooreView Answer on Stackoverflow
Solution 17 - VimSilvanusView Answer on Stackoverflow