Add close all other tabs shortcut

This commit is contained in:
Yan Lin 2025-09-12 23:00:04 +02:00
parent d1b3891e7e
commit 3e40d04572

View file

@ -240,6 +240,12 @@
action = ":bp|bd #<CR>"; action = ":bp|bd #<CR>";
options = { desc = "Close current buffer"; }; options = { desc = "Close current buffer"; };
} }
{
mode = "n";
key = "<leader>X";
action = ":lua close_other_buffers()<CR>";
options = { desc = "Close all buffers except current"; };
}
# System integration # System integration
{ {
@ -317,6 +323,25 @@
} }
} }
-- Close all buffers except current (preserving NvimTree and other special buffers)
function close_other_buffers()
local current_buf = vim.api.nvim_get_current_buf()
local buffers = vim.api.nvim_list_bufs()
for _, buf in ipairs(buffers) do
if buf ~= current_buf and vim.api.nvim_buf_is_valid(buf) then
local buftype = vim.api.nvim_buf_get_option(buf, 'buftype')
local filetype = vim.api.nvim_buf_get_option(buf, 'filetype')
-- Skip special buffers (NvimTree, terminals, quickfix, etc.)
if buftype == "" and filetype ~= "NvimTree" then
-- Only delete if it's a normal file buffer
vim.api.nvim_buf_delete(buf, { force = false })
end
end
end
end
-- Unicode-safe file operations -- Unicode-safe file operations
function open_file_with_system_app() function open_file_with_system_app()
local filepath = vim.fn.expand('%:p') local filepath = vim.fn.expand('%:p')