Skip to content

Commit be6b569

Browse files
committed
feat: add option to enter insert mode immediately in floating terminal
1 parent 9c44623 commit be6b569

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

lua/nvim_updater/init.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ function P.update_neovim(opts)
170170
filetype = "neovim_updater_term.updating",
171171
ispreupdate = false,
172172
autoclose = true,
173+
enter_insert = true,
173174
callback = function(results)
174175
if results.result_code ~= 0 then
175176
utils.notify("Neovim update failed with error code: " .. results.result_code, vim.log.levels.ERROR)
@@ -188,9 +189,6 @@ function P.update_neovim(opts)
188189
end
189190
end,
190191
})
191-
192-
-- Go to insert mode
193-
vim.cmd("startinsert")
194192
end
195193

196194
--- Remove the Neovim source directory or a custom one.

lua/nvim_updater/utils.lua

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ end
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
@@ -184,7 +185,8 @@ end
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

Comments
 (0)