Fix terminal cleanup when Claude process exits#106
Conversation
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.
📝 WalkthroughWalkthroughEnhanced terminal buffer cleanup in Claude Code by improving the TermClose autocommand callback to use proper callback signatures and implementing more aggressive cleanup that closes windows and deletes buffers when invalid states are detected. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Important Action Needed: IP Allowlist UpdateIf your organization protects your Git platform with IP whitelisting, please add the new CodeRabbit IP address to your allowlist:
Reviews will stop working after February 8, 2026 if the new IP is not added to your allowlist. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@lua/claude-code/file_refresh.lua`:
- Around line 103-105: The TermOpen autocmd uses the anchored pattern
'claude%-code$' while TermClose uses the unanchored 'claude%-code', causing
saved_updatetime to not be set for multi-instance buffers; update the TermOpen
autocmd (where saved_updatetime is set) to use the same unanchored pattern
'claude%-code' (or alternatively make TermClose use the anchored pattern) so
both autocmds consistently match multi-instance buffers and saved_updatetime is
always set/cleaned up.
| callback = function(args) | ||
| local buf_name = vim.api.nvim_buf_get_name(args.buf) | ||
| if buf_name:match('claude%-code') then |
There was a problem hiding this comment.
Pattern inconsistency between TermOpen and TermClose.
The TermClose autocmd now uses claude%-code (substring match) at line 105, but the TermOpen autocmd at line 91 still uses claude%-code$ (end anchor). This inconsistency means:
- For multi-instance buffers named
claude-code-<path>, TermClose will match and clean up correctly - But TermOpen won't match them, so
saved_updatetimewon't be set when opening multi-instance terminals
Consider aligning the patterns:
Proposed fix
vim.api.nvim_create_autocmd('TermOpen', {
group = augroup,
pattern = '*',
callback = function()
local buf = vim.api.nvim_get_current_buf()
local buf_name = vim.api.nvim_buf_get_name(buf)
- if buf_name:match('claude%-code$') then
+ if buf_name:match('claude%-code') then
claude_code.claude_code.saved_updatetime = vim.o.updatetime
vim.o.updatetime = config.refresh.updatetime
end
end,🤖 Prompt for AI Agents
In `@lua/claude-code/file_refresh.lua` around lines 103 - 105, The TermOpen
autocmd uses the anchored pattern 'claude%-code$' while TermClose uses the
unanchored 'claude%-code', causing saved_updatetime to not be set for
multi-instance buffers; update the TermOpen autocmd (where saved_updatetime is
set) to use the same unanchored pattern 'claude%-code' (or alternatively make
TermClose use the anchored pattern) so both autocmds consistently match
multi-instance buffers and saved_updatetime is always set/cleaned up.
Summary
Noticed this when running with a floating window.
Fixes two issues when the Claude process exits (via ctrl-c, ctrl-d, or pressing Enter after "Process exited"):
"E95: Buffer with this name already exists" error on toggle - When toggling after the process had exited,
is_valid_terminal_buffer()correctly detected the dead terminal and cleared the instance reference, but the old buffer wasn't deleted. The new instance then failed when trying to use the same buffer name.Empty floating window lingers after exit - Pressing Enter after "Process exited" would close the terminal job but leave the floating window open with an empty buffer.
Changes
terminal.lua: In
toggle(), when detecting a dead terminal buffer, close any windows showing it and delete the buffer before creating a new instance.file_refresh.lua: In
TermCloseautocmd, clean up instance tracking, close the floating window, and delete the buffer.Test plan
:ClaudeCodein floating window modeSummary by CodeRabbit
Release Notes