How can I close all buffers in Vim except the one I am currently editing?
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.
I was able to do this pretty easily like this:
:%bd|e#
If you don´t care the current one, is more simple to do something like (no script needing):
1,100bd
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
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
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.