Skip to content

[idea] <leader>bd smart buffer delete — close buffer without closing window #295

Description

@stanfish06

What

Add a <leader>bd keymap that deletes (unloads) the current buffer but keeps the window open, switching to the alternate buffer (#) or the next buffer as a fallback.

Where

lua/config/options.lua — alongside the existing H/L buffer navigation keymaps (lines 27–28).

Why it matters

The built-in :bd (and vim.cmd("bdelete")) closes the buffer and collapses the window. In a multi-split or multi-tab layout this is disorienting — the layout changes even though the intent was just to discard the current file.

The existing keymaps H/L navigate between buffers without closing, but there is no single binding to say "I'm done with this buffer, show me something else and discard this one." The workaround — L then :bd # — is two steps and easy to get wrong.

Recommended action

Add to lua/config/options.lua:

vim.keymap.set("n", "<leader>bd", function()
    local cur = vim.fn.bufnr()
    local alt = vim.fn.bufnr("#")
    -- switch to a sensible buffer before deleting the current one
    if alt > 0 and alt ~= cur and vim.fn.buflisted(alt) == 1 then
        vim.cmd("buffer " .. alt)
    else
        vim.cmd("bnext")
    end
    -- delete the buffer we just left (not the one now displayed)
    if vim.fn.buflisted(cur) == 1 then
        vim.cmd("bdelete " .. cur)
    end
end, { desc = "Delete buffer, keep window" })

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions