bufdel.nvim is a neovim plugin that helps you delete buffers without changing windows layout.
- Requirements
- Installation
- Usage
- User Commands
- Interactive Prompts
- User Autocmds
- Credits
- Comparison with Existing Plugins
- Self-Promotion
- License
- Neovim >= 0.10.0
Using nvim-plug
require('plug').add({
'wsdjeg/bufdel.nvim',
})Using lazy.nvim
{
'wsdjeg/bufdel.nvim',
cmd = { 'Bdelete', 'Bwipeout' },
keys = {
{ '<leader>bd', '<cmd>Bdelete<cr>', desc = 'Delete buffer' },
{ '<leader>bD', '<cmd>Bdelete!<cr>', desc = 'Force delete buffer' },
{ '<leader>bw', '<cmd>Bwipeout<cr>', desc = 'Wipeout buffer' },
},
}Using packer.nvim
use('wsdjeg/bufdel.nvim')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:
- A single integer (buffer number),
0for 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 modifiedignore_user_events: skip triggeringUser BufDelPreandUser BufDelPosteventsswitch: specify which buffer to switch to after deletion
Default Behavior:
The Lua API and user commands have different defaults for switch:
| Context | Default switch |
Behavior |
|---|---|---|
Lua API delete() |
nil |
Let Neovim decide |
:Bdelete / :Bwipeout |
'lastused' |
Switch to most recently used buffer |
Examples:
-
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)
-
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
-
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
trueto mark the buffer for deletion - Return
falseornilto keep the buffer - This allows conditional deletion based on buffer state
-
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
-
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 })
bufdel.nvim provides two user commands :Bdelete and :Bwipeout, which work the same as :bdelete and :bwipeout,
but these commands preserve the window layout.
| Command | Description |
|---|---|
:Bdelete [buf...] |
Delete buffer(s) |
:Bdelete! [buf...] |
Force delete (discard changes) |
:Bwipeout [buf...] |
Wipe buffer(s) completely |
:Bwipeout! [buf...] |
Force wipeout (discard changes) |
Examples:
- Delete the current buffer
:Bdelete - Delete a specific buffer by buffer number
:Bdelete 3 - Delete multiple buffers
:Bdelete 2 5 7 - Delete a range of buffers
:3,6Bdelete - Force delete without saving
:Bdelete!
Features:
<Tab>completion for buffer names!bang modifier to force deletion- Range support (
:3,6Bdelete) - Multiple buffer arguments
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 the same behavior as:bdeleteand:bwipeout. In this case, please use the buffer number instead::Bdelete <bufnr>
When deleting a buffer without force=true (or without ! in commands), bufdel.nvim shows interactive prompts in certain situations:
When attempting to delete a buffer with unsaved changes:
save changes to "filename"? Yes/No/Cancel
- Press
y(Yes): Save changes and delete the buffer - Press
n(No): Discard changes and force delete - Press any other key: Cancel the deletion
When attempting to delete a terminal buffer with a running job:
Terminal buffer N is still running, killed? Yes/No
- Press
y(Yes): Kill the terminal job and delete the buffer - Press any other key: Keep the terminal running (deletion cancelled)
bufdel.nvim triggers two user autocmds when deleting a buffer: User BufDelPre and User BufDelPost.
Here is an example to handle 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.