Skip to content

Instantly share code, notes, and snippets.

@nuffin
Last active November 30, 2024 08:08
Show Gist options
  • Save nuffin/da2ea7e7e8dc41812606ef50f9a3bc66 to your computer and use it in GitHub Desktop.
Save nuffin/da2ea7e7e8dc41812606ef50f9a3bc66 to your computer and use it in GitHub Desktop.
DeleteAllBuffersExceptCurrent() and key bindings for VIM9.1
" Check if the function is already defined, then delete it before redefining it
if exists('*DeleteAllBuffersExceptCurrent')
delfunction DeleteAllBuffersExceptCurrent
endif
" Define a function in Vim 9.1 to delete all buffers except the current one
def DeleteAllBuffersExceptCurrent(check_empty: bool)
var current_buffer = bufnr('%')
for buffer in getbufinfo()
# echomsg buffer
if buffer.bufnr != current_buffer && buffer.listed && !buffer.changed && (!check_empty || (empty(buffer.windows) && empty(buffer.popups)))
# echomsg buffer
execute 'bdelete ' .. buffer.bufnr
endif
endfor
enddef
" Delete buffers that not in any window or popup
nnoremap <silent> fbde :call DeleteAllBuffersExceptCurrent(v:true)<CR>
" Delete buffers except current active
nnoremap <silent> fbdex :call DeleteAllBuffersExceptCurrent(v:false)<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment