Last active
November 30, 2024 08:08
-
-
Save nuffin/da2ea7e7e8dc41812606ef50f9a3bc66 to your computer and use it in GitHub Desktop.
DeleteAllBuffersExceptCurrent() and key bindings for VIM9.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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