Skip to content

Commit 08ac036

Browse files
committed
Fix terminal cleanup when Claude process exits
When the Claude process exits (via ctrl-c, ctrl-d, or pressing Enter after "Process exited"), the plugin now properly cleans up: 1. In toggle(): When detecting a dead terminal buffer, close any windows showing it and delete the buffer before creating a new instance. This fixes "E95: Buffer with this name already exists" error. 2. In TermClose autocmd: Clean up instance tracking, close the floating window, and delete the buffer. This prevents an empty floating window from lingering after the terminal exits.
1 parent c9a31e5 commit 08ac036

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

lua/claude-code/file_refresh.lua

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,32 @@ function M.setup(claude_code, config)
9696
desc = 'Set shorter updatetime when Claude Code is open',
9797
})
9898

99-
-- When Claude Code closes, restore normal updatetime
99+
-- When Claude Code closes, restore normal updatetime and clean up
100100
vim.api.nvim_create_autocmd('TermClose', {
101101
group = augroup,
102102
pattern = '*',
103-
callback = function()
104-
local buf_name = vim.api.nvim_buf_get_name(0)
105-
if buf_name:match('claude%-code$') then
103+
callback = function(args)
104+
local buf_name = vim.api.nvim_buf_get_name(args.buf)
105+
if buf_name:match('claude%-code') then
106106
vim.o.updatetime = claude_code.claude_code.saved_updatetime
107+
-- Clean up instance tracking and close window
108+
for instance_id, bufnr in pairs(claude_code.claude_code.instances) do
109+
if bufnr == args.buf then
110+
claude_code.claude_code.instances[instance_id] = nil
111+
break
112+
end
113+
end
114+
-- Close windows and delete buffer after a short delay to allow TermClose to complete
115+
vim.schedule(function()
116+
local win_ids = vim.fn.win_findbuf(args.buf)
117+
for _, win_id in ipairs(win_ids) do
118+
pcall(vim.api.nvim_win_close, win_id, true)
119+
end
120+
pcall(vim.api.nvim_buf_delete, args.buf, { force = true })
121+
end)
107122
end
108123
end,
109-
desc = 'Restore normal updatetime when Claude Code is closed',
124+
desc = 'Restore normal updatetime and clean up when Claude Code is closed',
110125
})
111126
end
112127

lua/claude-code/terminal.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,14 @@ function M.toggle(claude_code, config, git)
394394

395395
-- Validate existing buffer
396396
if bufnr and not is_valid_terminal_buffer(bufnr) then
397-
-- Buffer is no longer a valid terminal, reset
397+
-- Buffer is no longer a valid terminal, clean up and reset
398+
-- Close any windows showing this buffer
399+
local win_ids = vim.fn.win_findbuf(bufnr)
400+
for _, win_id in ipairs(win_ids) do
401+
pcall(vim.api.nvim_win_close, win_id, true)
402+
end
403+
-- Delete the old buffer to free up the name
404+
pcall(vim.api.nvim_buf_delete, bufnr, { force = true })
398405
claude_code.claude_code.instances[instance_id] = nil
399406
bufnr = nil
400407
end

0 commit comments

Comments
 (0)