Skip to content

Instantly share code, notes, and snippets.

@husa
Last active December 16, 2024 16:19
Show Gist options
  • Save husa/302b82a15ebc548bf429832dd7ba6f2f to your computer and use it in GitHub Desktop.
Save husa/302b82a15ebc548bf429832dd7ba6f2f to your computer and use it in GitHub Desktop.
(nvim) Delete empty buffers
local empty_buf_delete_augroup = vim.api.nvim_create_augroup("emptybufdelete", {})
vim.api.nvim_create_autocmd({ "BufLeave" }, {
pattern = "*",
group = empty_buf_delete_augroup,
callback = function()
local buffers = vim.api.nvim_list_bufs()
for _, buf in ipairs(buffers) do
if
vim.api.nvim_buf_is_loaded(buf)
and vim.api.nvim_buf_get_name(buf) == ""
and vim.api.nvim_buf_get_option(buf, "buftype") == ""
and vim.api.nvim_buf_get_option(buf, "buflisted")
then
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
if #lines == 1 and #lines[1] == 0 then
vim.api.nvim_buf_delete(buf, {})
print("deleted empty bufffer")
end
end
end
end,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment