Skip to content

[idea] Auto-enter insert mode when navigating into a terminal buffer #148

@stanfish06

Description

@stanfish06

What

Add a BufEnter/WinEnter autocmd that automatically switches to insert (terminal) mode whenever the cursor lands in a terminal buffer. This removes the need to press i every time you return to the <leader>, terminal split.

Where

lua/config/options.lua — near the existing terminal keymap (<leader>, and <Esc><Esc> at lines 20–21).

Why it matters

The config opens a terminal with <leader>, and exits terminal mode with <Esc><Esc>. The missing ergonomic link is re-entry: after switching to another window and back, you always have to press i to resume typing. With H/L buffer cycling and <C-w> window switching both active, re-entering insert mode manually gets repetitive.

Suggested implementation

vim.api.nvim_create_autocmd({ "BufEnter", "WinEnter" }, {
    callback = function()
        if vim.bo.buftype == "terminal" then
            vim.cmd("startinsert")
        end
    end,
})

Notes

  • This does not affect <Esc><Esc> — that keymap exits terminal mode to normal mode within the terminal buffer, and the autocmd only fires on buffer/window enter, not on mode changes.
  • If the terminal is used for reading output (e.g. after :Compile), the auto-insert can be briefly annoying. A guard like if vim.fn.mode() ~= 'n' then return end or a buffer-local opt-out (similar to the format-on-save toggle pattern) could mitigate this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions