'Vim: Close All Buffers But This One

How can I close all buffers in Vim except the one I am currently editing?

vim


Solution 1:[1]

You could use this script from vim.org:

http://www.vim.org/scripts/script.php?script_id=1071

Just put it to your .vim/plugin directory and then use :BufOnly command to close all buffers but the active one. You could also map it elsewhere you like in your .vimrc.

Source on Github (via vim-scripts mirror): https://github.com/vim-scripts/BufOnly.vim/blob/master/plugin/BufOnly.vim

Solution 2:[2]

I was able to do this pretty easily like this:

:%bd|e#

Solution 3:[3]

Try this

bufdo bd

bufdo runs command for all buffers

http://vim.wikia.com/wiki/Run_a_command_in_multiple_buffers

Solution 4:[4]

If you don“t care the current one, is more simple to do something like (no script needing):

1,100bd

Solution 5:[5]

I do this

:w | %bd | e#

My favorite if I just want my current buffer open and close all others.

How it works: first write current buffer's changes, then close all open buffers, then reopen the buffer I was currently on. In Vim, the | chains the execution of commands together. If your buffer is up to date the above can be shortened to :%bd | e#

Solution 6:[6]

Building on juananruiz's answer.

Make a small change in the buffer you want to keep, then

:1,1000bd

The command bd (buffer delete) will not delete any buffers with unsaved changes. This way you can keep the current (changed) file in the buffer list.

Edit: Please notice that this will also delete your NERDTreeBuffer. You can get it back with :NERDTree

Solution 7:[7]

Note: As mentioned in the comments, this closes windows and not buffers.

By using

:on[ly][!]

and

:h only

Solution 8:[8]

I put this in my .vimrc file

nnoremap <leader>ca :w <bar> %bd <bar> e# <bar> bd# <CR>

then your leader + ca (close all) close all the buffers except the current one.

What it does is

:w - save current buffer

%bd - close all the buffers

e# - open last edited file

bd# - close the unnamed buffer

Solution 9:[9]

Here's what I do. So I like to keep my cursor position after removing all buffers and most of the solutions above just ignores this fact. I also think remapping the command is better than typing it so Here I use <leader>bd to remove all buffers and jump back to my original cursor position.

noremap <leader>bd :%bd\|e#\|bd#<cr>\|'"

%bd = delete all buffers.

e# = open the last buffer for editing (Which Is the buffer I'm working on).

bd# to delete the [No Name] buffer that gets created when you use %bd.

The pipe in between just does one command after another. You've gotta escape it though using \|

'" = keep my cursor position.

Solution 10:[10]

Closing all open buffers:

silent! execute "1,".bufnr("$")."bd"

Closing all open buffers except for the current one:

function! CloseAllBuffersButCurrent()
  let curr = bufnr("%")
  let last = bufnr("$")

  if curr > 1    | silent! execute "1,".(curr-1)."bd"     | endif
  if curr < last | silent! execute (curr+1).",".last."bd" | endif
endfunction

Add this function to .vimrc and call it using :call CloseAllBuffersButCurrent().

Convenience map:

nmap <Leader>\c :call CloseAllBuffersButCurrent()<CR>

Solution 11:[11]

There's a plugin that does exactly this and a bit more!

Check out close-buffers.vim

Solution 12:[12]

so this is an old question but it helped me get some ideas for my project. in order to close all buffers but the one you are currently using, use;

map <leader>o :execute "%bd\|e#"<CR>

Solution 13:[13]

I like 1,100bd (suggested by juananruiz) which seems to work for me.

I added a quit! to my mapping to give me

nnoremap <leader>bd :1,100bd<CR>
nnoremap <leader>bdq :1,100bd<CR>:q!<CR>

This kills all the buffers and shuts down Vim, which is what I was looking for mostly.

Solution 14:[14]

Solution 15:[15]

I combined Alejandro's comment with badteeth's comment:

command! Bonly silent execute "%bd|norm <C-O>"
  • The norm <C-O> jumps to the last position in the jump list, which means where the cursor was before the %bd.
  • I used silent instead of silent!. That way, if any open buffers are modified, Vim prints an error message so I know what happened. The modified buffers stay open in my tests.

Unrelated: this is my 500th answer!

Solution 16:[16]

nnoremap <leader>x :execute '%bdelete\|edit #\|normal `"'\|bdelete#<CR>
  • Close all buffers (side-effect creates new empty buffer)
  • Open last buffer
  • Jump to last edit position in buffer
  • Delete empty buffer

Solution 17:[17]

  • The answer with highest votes will reopen the buffer, it will lose the current line we are working on.
  • Close then reopen will induce a flush on screen
function! CloseOtherBuffer()
    let l:bufnr = bufnr()
    execute "only"
    for buffer in getbufinfo()
        if !buffer.listed
            continue
        endif
        if buffer.bufnr == l:bufnr
            continue
        else
            if buffer.changed
                echo buffer.name . " has changed, save first"
                continue
            endif
            let l:cmd = "bdelete " . buffer.bufnr
            execute l:cmd
        endif
    endfor
endfunction

let mapleader = ','
nnoremap <leader>o :call CloseOtherBuffer()<CR>

Previous code will take effect when you are on the target buffer and press , + o. It will close all other buffers except current one.

It iterates all the buffers and close all the buffer number which is not equal to current buffer number.