Vim: Close All Buffers But This One

Vim

Vim Problem Overview


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

Vim Solutions


Solution 1 - Vim

I was able to do this pretty easily like this:

:%bd|e#

Solution 2 - Vim

Try this

bufdo bd

bufdo runs command for all buffers

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

Solution 3 - Vim

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 4 - Vim

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

1,100bd

Solution 5 - Vim

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 - Vim

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 - Vim

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

By using

:on[ly][!]

and

:h only

Solution 8 - Vim

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 - Vim

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 - Vim

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 - Vim

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

Check out close-buffers.vim

Solution 12 - Vim

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 - Vim

Solution 14 - Vim

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 15 - Vim

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 - Vim

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 - Vim

  • 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.

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
QuestionRafidView Question on Stackoverflow
Solution 1 - VimOldTimeGuitarGuyView Answer on Stackoverflow
Solution 2 - VimgayavatView Answer on Stackoverflow
Solution 3 - VimVoYView Answer on Stackoverflow
Solution 4 - VimjuananruizView Answer on Stackoverflow
Solution 5 - VimiamnotsamView Answer on Stackoverflow
Solution 6 - VimcutemachineView Answer on Stackoverflow
Solution 7 - VimklokopView Answer on Stackoverflow
Solution 8 - VimYulinView Answer on Stackoverflow
Solution 9 - Vim0xMHView Answer on Stackoverflow
Solution 10 - VimmynymlView Answer on Stackoverflow
Solution 11 - VimNiko BellicView Answer on Stackoverflow
Solution 12 - VimBrendanArmsView Answer on Stackoverflow
Solution 13 - VimMatt WalshView Answer on Stackoverflow
Solution 14 - VimLazyRiverYogiView Answer on Stackoverflow
Solution 15 - VimcxwView Answer on Stackoverflow
Solution 16 - VimdrzelView Answer on Stackoverflow
Solution 17 - Vimchunyang.wenView Answer on Stackoverflow