Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I close a buffer without closing the window?

Tags:

vim

People also ask

How do I close a window in Vim?

^Wc (or :close[!] ) closes the current window. If the hidden option is set and this is the last window referencing this file, Vim closes the window and the buffer is hidden. If this window is on a tab page and is the last window for that tab page, the window and the tab page are closed.

How do I close all buffers in Vim?

Just put it to your . vim/plugin directory and then use :BufOnly command to close all buffers but the active one.

What is a Vim buffer?

A buffer is an area of Vim's memory used to hold text read from a file. In addition, an empty buffer with no associated file can be created to allow the entry of text. The :e filename command can edit an existing file or a new file.


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)


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.


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.


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

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

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