I'm currently trying to set this up using the following autocommand, but it is not working for some reason.

-- Delete unnamed, unmodified buffers when they are hidden.
vim.api.nvim_create_autocmd("BufHidden", {
  group = vim.api.nvim_create_augroup("UserAutoClose", {}),
  callback = function(args)
    if vim.bo[args.buf].buftype == "nofile" then
      vim.api.nvim_buf_delete(args.buf, {})

Furthermore, this autocommand produces the following error when closing a tab (with :tabclose) that has only one attached buffer.

Error detected while processing BufHidden Autocommands for "*":
E937: Attempt to delete a buffer that is in use: [No Name]
Error executing lua callback: /home/pwnalone/.config/chad/lua/custom/autocmds.lua:6: Failed to unload buffer.
stack traceback:
        [C]: in function 'nvim_buf_delete'
        /home/pwnalone/.config/chad/lua/custom/autocmds.lua:6: in function </home/pwnalone/.config/chad/lua/custom/autocmds.lua:4>

NOTE: I know that buffers are not scoped to individual tabs by default in Neovim, but I'm using the NvChad distribution as my base configuration, which provides the ability to group buffers by tab.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.