bufdel.nvim is a neovim plugin that helps you delete buffers without changing windows layout.
Using nvim-plug
require('plug').add({
'wsdjeg/bufdel.nvim',
})
Using luarocks
luarocks install bufdel.nvim
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:
0 for current buffer.bufnr(str).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 modifiedignore_user_events: skip triggering User BufDelPre and User BufDelPost eventsswitch: specify which buffer to switch to after deletionExamples:
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 })
wipe = true, the buffer is wiped (:bwipeout):bdelete)Delete multiple buffers
Delete multiple buffers at once by passing a list of buffer numbers.
require('bufdel').delete({ 2, 3, 5 }, { wipe = true })
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 })
true to mark the buffer for deletionfalse or nil to keep the bufferDelete buffers whose names match a regular expression:
require('bufdel').delete('.txt$', { wipe = true })
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:
Use built-in switch strategies
require('bufdel').delete(filter, {
wipe = true,
switch = 'alt', -- alternate buffer (#)
})
Supported built-in values:
Specify a buffer number directly
require('bufdel').delete(filter, {
wipe = true,
switch = 3, -- switch to bufnr = 3
})
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:
:Bdelete
:Bdelete 3
:Bdelete 2 5 7
: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>
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.
The core logic of bufdel.nvim is derived from bufdelete.nvim.
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.
Like this plugin? Star the repository on GitHub.
Love this plugin? Follow me on GitHub or Twitter.
This project is licensed under the GPL-3.0 License.