How can I close all buffers in Vim except the one I am currently editing?
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>