fix: check buffer validity in autocmd to prevent keymap.set() throwing

the buffer that triggered the autocmd could be gone by the time
vim.schedule runs keymap.set, e.g. with transient buffers created by
nvim_get_option_value
This commit is contained in:
akioweh 2026-05-16 05:33:55 +01:00
parent 7c1301b895
commit de3cd1fb32
No known key found for this signature in database

View file

@ -74,6 +74,10 @@ vim.api.nvim_create_autocmd("FileType", {
callback = function(event)
vim.bo[event.buf].buflisted = false
vim.schedule(function()
-- without this, vim.keymap.set below may throw on transient buffers
if not vim.api.nvim_buf_is_valid(event.buf) then
return
end
vim.keymap.set("n", "q", function()
vim.cmd("close")
pcall(vim.api.nvim_buf_delete, event.buf, { force = true })