225
votes

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

16
I fount this link - klokop

16 Answers

64
votes

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

196
votes
186
votes

I was able to do this pretty easily like this:

:%bd|e#
60
votes

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

1,100bd
54
votes

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#

26
votes

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

16
votes

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

By using

:on[ly][!]

and

:h only
11
votes

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

10
votes

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>
8
votes

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.

6
votes

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

Check out close-buffers.vim

1
votes

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>

0
votes

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.

0
votes

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!

0
votes
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