@@ -199,6 +199,21 @@ local function get_default_terminal_options()
199199 }
200200end
201201
202+ --- Mark the current buffer (just created by tabnew) as ephemeral so it auto-wipes when hidden.
203+ local function mark_tabnew_buffer_ephemeral ()
204+ local buf = vim .api .nvim_get_current_buf ()
205+ local ok_name , name = pcall (vim .api .nvim_buf_get_name , buf )
206+ local ok_mod , modified = pcall (vim .api .nvim_buf_get_option , buf , " modified" )
207+ local ok_lc , linecount = pcall (function ()
208+ return vim .api .nvim_buf_line_count (buf )
209+ end )
210+ if ok_name and ok_mod and ok_lc then
211+ if (name == nil or name == " " ) and modified == false and linecount <= 1 then
212+ pcall (vim .api .nvim_buf_set_option , buf , " bufhidden" , " wipe" )
213+ end
214+ end
215+ end
216+
202217--- Display existing Claude Code terminal in new tab
203218--- @return number original_tab The original tab number
204219--- @return number ? terminal_win Terminal window in new tab
@@ -211,13 +226,15 @@ local function display_terminal_in_new_tab()
211226 local terminal_ok , terminal_module = pcall (require , " claudecode.terminal" )
212227 if not terminal_ok then
213228 vim .cmd (" tabnew" )
229+ mark_tabnew_buffer_ephemeral ()
214230 local new_tab = vim .api .nvim_get_current_tabpage ()
215231 return original_tab , nil , false , new_tab
216232 end
217233
218234 local terminal_bufnr = terminal_module .get_active_terminal_bufnr ()
219235 if not terminal_bufnr or not vim .api .nvim_buf_is_valid (terminal_bufnr ) then
220236 vim .cmd (" tabnew" )
237+ mark_tabnew_buffer_ephemeral ()
221238 local new_tab = vim .api .nvim_get_current_tabpage ()
222239 return original_tab , nil , false , new_tab
223240 end
@@ -232,22 +249,9 @@ local function display_terminal_in_new_tab()
232249 end
233250
234251 vim .cmd (" tabnew" )
252+ mark_tabnew_buffer_ephemeral ()
235253 local new_tab = vim .api .nvim_get_current_tabpage ()
236254
237- -- Mark the initial, unnamed buffer in the new tab as ephemeral to avoid leaks
238- -- When this buffer gets hidden (replaced or tab closed), wipe it automatically.
239- local initial_buf = vim .api .nvim_get_current_buf ()
240- local name_ok , initial_name = pcall (vim .api .nvim_buf_get_name , initial_buf )
241- local mod_ok , initial_modified = pcall (vim .api .nvim_buf_get_option , initial_buf , " modified" )
242- local linecount_ok , initial_linecount = pcall (function ()
243- return vim .api .nvim_buf_line_count (initial_buf )
244- end )
245- if name_ok and mod_ok and linecount_ok then
246- if (initial_name == nil or initial_name == " " ) and initial_modified == false and initial_linecount <= 1 then
247- pcall (vim .api .nvim_buf_set_option , initial_buf , " bufhidden" , " wipe" )
248- end
249- end
250-
251255 local terminal_config = config .terminal or {}
252256 local split_side = terminal_config .split_side or " right"
253257 local split_width = terminal_config .split_width_percentage or 0.30
0 commit comments