wsdjeg/bufdel.nvim

github github
buffers
stars 10
issues 2
subscribers 0
forks 2
CREATED

UPDATED


bufdel.nvim

bufdel.nvim is a neovim plugin that helps you delete buffers without changing windows layout.

GitHub License GitHub Issues or Pull Requests GitHub commit activity GitHub Release luarocks

Installation

Using nvim-plug

require('plug').add({
    'wsdjeg/bufdel.nvim',
})

Using luarocks

luarocks install bufdel.nvim

Usage

This plugin exposes a core delete function, which accepts two parameters: buffers and opt.

The buffers parameter specifies which buffers should be deleted. It supports multiple forms:

  • A single integer (buffer number), 0 for current buffer.
  • A string: it is first resolved to a buffer number using bufnr(str).
    If a valid buffer number is returned, that buffer is deleted.
    Otherwise, the string is treated as a Vim regular expression and matched against buffer names.
  • A table of integers or strings (each string follows the same resolution rule as above)
  • A function, used to filter vim.api.nvim_list_bufs(). The function receives a buffer number as its argument and must return a boolean indicating whether the buffer should be deleted.

The opt parameter is a table used to control the deletion behavior. Supported keys include:

  • wipe: whether to wipe the buffer (bwipeout) instead of deleting it (bdelete)
  • force: force deletion even if the buffer is modified
  • ignore_user_events: skip triggering User BufDelPre and User BufDelPost events
  • switch: specify which buffer to switch to after deletion

Examples:

  1. Delete a specific buffer

    Delete a single buffer by providing its buffer number.
    This is useful when you already know exactly which buffer should be removed.

    require('bufdel').delete(2, { wipe = true })
    
    • The first argument is the buffer number to delete
    • The buffer must be valid
    • If wipe = true, the buffer is wiped (:bwipeout)
    • Otherwise, the buffer is deleted (:bdelete)
  2. Delete multiple buffers

    Delete multiple buffers at once by passing a list of buffer numbers.

    require('bufdel').delete({ 2, 3, 5 }, { wipe = true })
    
    • Each item in the list must be a valid buffer number
    • Invalid or already-deleted buffers are ignored
    • Buffers are deleted in sequence
    • This is useful for batch-cleaning buffers
  3. Delete buffers using a filter function

    Delete buffers dynamically by providing a filter function.
    The function is called for each existing buffer and should return true if the buffer should be deleted.

    -- delete other saved buffers
    require('bufdel').delete(function(buf)
        return not vim.bo[buf].modified
            and vim.bo[buf].buflisted
            and buf ~= vim.api.nvim_get_current_buf()
    end, { wipe = true })
    
    • The filter function receives a buffer number
    • Return true to mark the buffer for deletion
    • Return false or nil to keep the buffer
    • This allows conditional deletion based on buffer state
  4. Delete buffers whose names match a regular expression:

    require('bufdel').delete('.txt$', { wipe = true })
    
    • The buffer name is matched against a Vim regular expression
    • Only buffers whose names satisfy the pattern will be deleted
    • This is useful for cleaning up temporary or generated files
  5. Specify the buffer to switch to after deletion

    By default, bufdel.nvim lets Neovim decide which buffer to display after a buffer is deleted.
    You can override this behavior using the switch option to explicitly control which buffer to switch to after deletion.

    Use a function to customize the switch logic (recommended)

    require('bufdel').delete(function(buf)
        return not vim.bo[buf].modified and vim.bo[buf].buflisted
    end, {
        wipe = true,
        switch = function(deleted_buf)
            -- Return a valid buffer number
            return vim.fn.bufnr('#') -- switch to the alternate buffer
        end,
    })
    

    When switch is a function:

    • It receives the deleted buffer number (bufnr)
    • It must return a target bufnr
    • If the returned buffer is invalid, the switch is ignored

    Use built-in switch strategies

    require('bufdel').delete(filter, {
        wipe = true,
        switch = 'alt', -- alternate buffer (#)
    })
    

    Supported built-in values:

    • "alt" – alternate buffer (#)
    • "current" – keep the current buffer
    • "lastused" - the last used buffer
    • "next" – next buffer
    • "prev" – previous buffer

    Specify a buffer number directly

    require('bufdel').delete(filter, {
        wipe = true,
        switch = 3, -- switch to bufnr = 3
    })
    

User Commands

bufdel.nvim also provides two user commands :Bdelete and :Bwipeout, which is just same as :delete and :bwipeout, but these user commands will not change the windows layout.

Examples:

  1. Delete the current buffer
    :Bdelete
    
  2. Delete a specific buffer by buffer number
    :Bdelete 3
    
  3. Delete multiple buffers
    :Bdelete 2 5 7
    
  4. Delete a range of buffers
    :3,6Bdelete
    

Note: Because of limitations in Vim’s Ex command parsing, buffer names that consist of digits only cannot be used with :Bdelete <bufname>. It is same behavior as :bdelete and :bwipeout. In this case, please use the buffer number instead:

:Bdelete <bufnr>

User Autocmds

bufdel.nvim triggers two user autocmds when delete a buffer, User BufDelPre and User BufDelPost. here is an example to handled these events:

local mygroup = vim.api.nvim_create_augroup("bufdel_custom", { clear = true })
vim.api.nvim_create_autocmd({ "User" }, {
    group = mygroup,
    pattern = "BufDelPre",
    callback = function(ev)
        --- the deleted buffer number is saved in ev.data.buf
    end,
})
vim.api.nvim_create_autocmd({ "User" }, {
    group = mygroup,
    pattern = "BufDelPost",
    callback = function(ev)
        --- the deleted buffer number is saved in ev.data.buf
    end,
})

The User BufDelPost event will not be triggered if failed to delete the buffer.

Credits

The core logic of bufdel.nvim is derived from bufdelete.nvim.

Comparison with Existing Plugins

Below is a minimal comparison of bufdel.nvim and several other buffer-deletion plugins based on the features the author personally uses:

Feature / Plugin bufdel.nvim bufdelete.nvim nvim-bufdel snacks.bufdelete mini.bufremove
Preserve window layout
Delete by bufnr
Delete by bufname
User Command
Lua filter function
Regex buffer matching
Post-delete buffer switch
User autocmd hooks

Some plugins are actively maintained. For a more detailed comparison, you’re encouraged to try them and see which best fits your setup.
If you notice any inaccuracies or mistakes in the comparison, please feel free to open an issue.

Self-Promotion

Like this plugin? Star the repository on GitHub.

Love this plugin? Follow me on GitHub or Twitter.

License

This project is licensed under the GPL-3.0 License.