akinsho / toggleterm.nvim

A neovim lua plugin to help easily manage multiple terminal windows
GNU General Public License v3.0
4.13k stars 168 forks source link

Custom terminal doesn't start in insert mode #401

Closed sho-87 closed 1 year ago

sho-87 commented 1 year ago

I have a utils.lua file that defines and exports a custom terminal:

local M = {}

local Terminal = require("toggleterm.terminal").Terminal
local lazygit = Terminal:new({
    cmd = "lazygit",
    hidden = true,
    on_open = function(term)
        vim.cmd("startinsert!")
    end,
    close_on_exit = true,
    direction = "float",
    float_opts = {
        border = "single",
    },
})

local function toggle_lazygit()
    lazygit:toggle()
end
M.toggle_lazygit = toggle_lazygit

return M

Then I have a keymap that calls the toggle function, which all seems to work fine and it starts lazygit in insert mode as expected:

map("n", "<leader>g", require('utils').toggle_lazygit)

However, I'm calling the same function in the on_click of a lualine component, but in that case it starts in normal mode instead of insert:

lualine_b = {
    { "branch", on_click = require("utils").toggle_lazygit },
    { "diff", on_click = require("utils").toggle_lazygit },
},

My toggleterm config:

    require("toggleterm").setup({
        shell = "powershell.exe",
        start_in_insert = true,
        close_on_exit = true,
        shade_terminals = true,
        hide_numbers = true,
        autochdir = false,
        direction = "float",
    })

Is this an issue with my code or a bug with custom terminals?

akinsho commented 1 year ago

@sho-87 I don't really know, the issue you've described here seems quite specific to your workflow/config and involves interactions with other plugins. I don't know why it might not work with lualine. You might need to try using something like vim.schedule to wrap the start insert. In either case I generally don't handle configuration debugging type issues since it's just me maintaining this so I need to focus on explicit bugs or particularly valuable feature requests