akinsho / toggleterm.nvim

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

[BUG] `insert_mappings=false` not working #605

Open Dioswilson opened 3 months ago

Dioswilson commented 3 months ago

Is there an existing issue for this?

Current Behavior

I have this configuration:

    config = function()
        require("toggleterm").setup({
            open_mapping = [[<leader>t]],
            start_in_insert = true,
            -- terminal_mappings = true,
            insert_mappings = false, -- whether or not the open mapping applies in insert mode
            direction = "horizontal",
            close_on_exit = true, -- close the terminal window when the process exits
        })
    end,

with leader being spacebar, when hitting spacebar to separate words it keeps waiting for the next key and only option it apears in which key is toggle term

Expected Behavior

insert_mappings=false should prevent me from hitting <leader>t to close the terminal while writing

Steps To Reproduce

  1. Copy plugin configuration
  2. Set spacebar as leader
  3. Open terminal with <leader>t]
  4. Hit spacebar

Environment

- OS: Archlinux 
- neovim version: 10.1 
- Shell: Zsh

Anything else?

No response

mbwilding commented 1 month ago

My fix was removing t mode from my manual mappings. When I leave insert mode in ToggleTerm I can use my bindings. But if that config above was your total config around ToggleTerm, then this won't help you.

before

vim.keymap.set({ "n", "t" }, "<leader>th", "<cmd>:1ToggleTerm direction=horizontal<CR>")
vim.keymap.set({ "n", "t" }, "<leader>tt", "<cmd>:2ToggleTerm direction=horizontal<CR>")
vim.keymap.set({ "n", "t" }, "<leader>tn", "<cmd>:3ToggleTerm direction=horizontal<CR>")
vim.keymap.set({ "n", "t" }, "<leader>ts", "<cmd>:4ToggleTerm direction=horizontal<CR>")
vim.keymap.set({ "n", "t" }, "<leader>tc", "<cmd>:ToggleTermToggleAll<CR>")

after

vim.keymap.set("n", "<leader>th", "<cmd>:1ToggleTerm direction=horizontal<CR>")
vim.keymap.set("n", "<leader>tt", "<cmd>:2ToggleTerm direction=horizontal<CR>")
vim.keymap.set("n", "<leader>tn", "<cmd>:3ToggleTerm direction=horizontal<CR>")
vim.keymap.set("n", "<leader>ts", "<cmd>:4ToggleTerm direction=horizontal<CR>")
vim.keymap.set("n", "<leader>tc", "<cmd>:ToggleTermToggleAll<CR>")

complete

return {
    "akinsho/nvim-toggleterm.lua",
    opts = {
        auto_scroll = true,
        close_on_exit = true,
        hide_numbers = true,
        insert_mappings = false, -- Relevant
        persist_size = true,
        shade_terminals = false,
        start_in_insert = true,
        terminal_mappings = false, --  Relevant
    },
    config = function(_, opts)
        local toggle_term = require("toggleterm")

        -- Windows
        if vim.fn.has("win32") == 1 then
            opts.shell_command = "pwsh.exe -NoLogo"
        end

        toggle_term.setup(opts)

        vim.keymap.set(
            "n",
            "<leader>th",
            "<CMD>:1ToggleTerm direction=horizontal<CR>",
            { desc = "ToggleTerm: Horizontal 1" }
        )
        vim.keymap.set(
            "n",
            "<leader>tt",
            "<CMD>:2ToggleTerm direction=horizontal<CR>",
            { desc = "ToggleTerm: Horizontal 2" }
        )
        vim.keymap.set(
            "n",
            "<leader>tn",
            "<CMD>:3ToggleTerm direction=horizontal<CR>",
            { desc = "ToggleTerm: Horizontal 3" }
        )
        vim.keymap.set(
            "n",
            "<leader>ts",
            "<CMD>:4ToggleTerm direction=horizontal<CR>",
            { desc = "ToggleTerm: Horizontal 4" }
        )
        vim.keymap.set("n", "<leader>tc", "<CMD>:ToggleTermToggleAll<CR>", { desc = "ToggleTerm: Toggle All" })

        function _G.set_terminal_keymaps()
            local opts = { buffer = 0 }
            vim.keymap.set("t", "<esc>", [[<C-\><C-n>]], opts)
            vim.keymap.set("t", "jk", [[<C-\><C-n>]], opts)
            vim.keymap.set("t", "<C-h>", [[<Cmd>wincmd h<CR>]], opts)
            vim.keymap.set("t", "<C-j>", [[<Cmd>wincmd j<CR>]], opts)
            vim.keymap.set("t", "<C-k>", [[<Cmd>wincmd k<CR>]], opts)
            vim.keymap.set("t", "<C-l>", [[<Cmd>wincmd l<CR>]], opts)
            vim.keymap.set("t", "<C-w>", [[<C-\><C-n><C-w>]], opts)
        end

        -- if you only want these mappings for toggle term use term://*toggleterm#* instead
        vim.cmd("autocmd! TermOpen term://* lua set_terminal_keymaps()")
    end,
}