167167--- Please use the `callback` function instead.
168168--- @field autoclose ? boolean Whether the terminal should be automatically closed (optional )
169169--- @field callback ? fun ( params ?: TerminalCloseParams ) Callback function to run after the terminal is closed
170+ --- @field enter_insert ? boolean Whether to enter insert mode immediately (optional )
170171
171172--- Callback parameter table for the floating terminal close event.
172173--- @class TerminalCloseParams
184185--- Please use the `callback` function instead.
185186--- @param autoclose ? boolean Whether the terminal should be automatically closed (optional if using positional arguments )
186187--- @param callback ? fun ( params ?: TerminalCloseParams ) Callback function to run after the terminal is closed
187- function U .open_floating_terminal (command_or_opts , filetype , ispreupdate , autoclose , callback )
188+ --- @param enter_insert ? boolean Whether to enter insert mode immediately (optional if using positional arguments )
189+ function U .open_floating_terminal (command_or_opts , filetype , ispreupdate , autoclose , callback , enter_insert )
188190 local opts
189191 local result_code = - 1 -- Indicates the command is still running
190192 local output_lines = {} -- Store terminal output lines
@@ -195,10 +197,11 @@ function U.open_floating_terminal(command_or_opts, filetype, ispreupdate, autocl
195197 else
196198 opts = {
197199 command = command_or_opts or " " ,
198- filetype = filetype or " floating.term " , -- Default filetype
200+ filetype = filetype or " FloatingTerm " ,
199201 ispreupdate = ispreupdate or false ,
200202 autoclose = autoclose or false ,
201203 callback = callback or nil ,
204+ enter_insert = enter_insert or false ,
202205 }
203206 end
204207
@@ -210,6 +213,7 @@ function U.open_floating_terminal(command_or_opts, filetype, ispreupdate, autocl
210213 callback = opts .callback or function ()
211214 return true
212215 end
216+ enter_insert = opts .enter_insert or false
213217
214218 -- Create a new buffer for the terminal, set it as non-listed and scratch
215219 local buf = vim .api .nvim_create_buf (false , true )
@@ -340,6 +344,19 @@ function U.open_floating_terminal(command_or_opts, filetype, ispreupdate, autocl
340344 end ,
341345 })
342346
347+ if enter_insert then
348+ -- Enter insert mode immediately
349+ vim .cmd (" startinsert" )
350+
351+ -- Create an autocmd to ensure insert mode when the window gets focus
352+ vim .api .nvim_create_autocmd (" WinEnter" , {
353+ buffer = buf ,
354+ callback = function ()
355+ vim .cmd (" startinsert" )
356+ end ,
357+ })
358+ end
359+
343360 -- Create an autocmd for the window closing callback
344361 if callback then
345362 local winid = tostring (win )
0 commit comments