@@ -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 })
111126end
112127
0 commit comments