akinsho / akinsho/toggleterm.nvim

Question :: (Re)-focus on open terminal window using it's count number

未关闭
#42 6 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看
bug enhancement
主要语言
Lua
星标
5.6k
派生
206
PR 合并指标
30 天内没有已合并 PR

描述

First of all, excellent plugin and thanks a million for putting effort in this (and other) plugins. I'm using nvim-toggleterm daily in my workflow with a few custom functions for my specific preferences.

One thing I haven't been able to figure out yet is how to navigate to a terminal window based on it's count number, regardless if the terminal window is already open (but not focused) or not opened (anymore).

- If the terminal window hasn't been opened yet or if I have previously closed the terminal window (but left it running in the background), i'm fine using `:toggle` to open the terminal window (again). However, if I focus on a non-terminal window and use `:toggle` to refer to the specific terminal window to focus on, it closes the already open terminal window and errors out with `Failed to close window: win id - [0-9]{1,4} does not exist` (mind the in-place regex).

I can of course move to the terminal window using, for instance, `2wincmd w`, or whatever number the window got assigned. It however requires me to look at the statusline to find out the window number to determine which one i'll have to move to, which obviously is less optimal than it could be ;-).

Is there a way to refocus on an already open terminal window by referring to it's number (set using `count = [0-9]+`) making use of an nvim-toggleterm function or similar ? This way I can hook a keymapping to it, preferably the same keymapping I already use to toggle it so I can keep using one keymapping per function.

For convenience, the nvim-toggleterm snippet i'm using with `NVIM v0.5.0-dev+61aefaf29` on MacOS here below:

nvim-toggleterm Lua

```lua
require('toggleterm').setup {
close_on_exit = true,
float_opts = {
border = 'curved'
},
persist_size = false
}

local terminal_default_float = require('toggleterm.terminal').Terminal:new({
count = 50,
direction = 'float',
on_open = function(term)
vim.api.nvim_buf_set_keymap(term.bufnr, 'n', 'q', 'close', { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(term.bufnr, 't', '', 'close', { noremap = true })
vim.wo.cursorcolumn = false
vim.wo.cursorline = false
vim.cmd('VimadeBufDisable')
vim.cmd("startinsert!")
end,
on_close = function(term)
vim.cmd("quit!")
end
})

local terminal_horizontal_one = require('toggleterm.terminal').Terminal:new({
count = 11,
direction = 'horizontal',
on_open = function(term)
vim.api.nvim_buf_set_keymap(term.bufnr, 'n', 'q', 'close', { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(term.bufnr, 't', '', 'close', { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(term.bufnr, 't', '', '', { noremap = true, silent = true })
vim.wo.cursorcolumn = false
vim.wo.cursorline = false
vim.cmd('VimadeBufDisable')
vim.cmd("startinsert!")
end,
on_close = function(term)
vim.cmd("quit!")
end
})

local terminal_horizontal_two = require('toggleterm.terminal').Terminal:new({
count = 12,
direction = 'horizontal',
on_open = function(term)
vim.api.nvim_buf_set_keymap(term.bufnr, 'n', 'q', 'close', { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(term.bufnr, 't', '', 'close', { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(term.bufnr, 't', '', '', { noremap = true, silent = true })
vim.wo.cursorcolumn = false
vim.wo.cursorline = false
vim.cmd('VimadeBufDisable')
vim.cmd("startinsert!")
end,
on_close = function(term)
vim.cmd("quit!")
end
})

local terminal_horizontal_three = require('toggleterm.terminal').Terminal:new({
count = 13,
direction = 'horizontal',
on_open = function(term)
vim.api.nvim_buf_set_keymap(term.bufnr, 'n', 'q', 'close', { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(term.bufnr, 't', '', 'close', { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(term.bufnr, 't', '', '', { noremap = true, silent = true })
vim.wo.cursorcolumn = false
vim.wo.cursorline = false
vim.cmd('VimadeBufDisable')
vim.cmd("startinsert!")
end,
on_close = function(term)
vim.cmd("quit!")
end
})

function _terminal_default_float_toggle()
terminal_default_float:toggle()
end

function _terminal_horizontal_one_toggle()
terminal_horizontal_one:toggle()
end

function _terminal_horizontal_two_toggle()
terminal_horizontal_two:toggle()
end

function _terminal_horizontal_three_toggle()
terminal_horizontal_three:toggle()
end

local terminal_tig = require('toggleterm.terminal').Terminal:new({
cmd = 'tig',
count = 60,
direction = 'float',
on_open = function(term)
vim.api.nvim_buf_set_keymap(term.bufnr, 'n', 'q', 'close', { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(term.bufnr, 't', '', 'close', { noremap = true })
vim.wo.cursorcolumn = false
vim.wo.cursorline = false
vim.cmd('VimadeBufDisable')
vim.cmd("startinsert!")
end,
on_close = function(term)
vim.cmd("quit!")
end
})

local terminal_lazygit = require('toggleterm.terminal').Terminal:new({
cmd = 'lazygit',
count = 61,
direction = 'float',
on_open = function(term)
vim.api.nvim_buf_set_keymap(term.bufnr, 'n', 'q', 'close', { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(term.bufnr, 't', '', 'close', { noremap = true })
vim.wo.cursorcolumn = false
vim.wo.cursorline = false
vim.cmd('VimadeBufDisable')
vim.cmd("startinsert!")
end,
on_close = function(term)
vim.cmd("quit!")
end
})

local terminal_k9s = require('toggleterm.terminal').Terminal:new({
cmd = 'k9s',
count = 62,
direction = 'float',
on_open = function(term)
vim.api.nvim_buf_set_keymap(term.bufnr, 'n', 'q', 'close', { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(term.bufnr, 't', '', 'close', { noremap = true })
vim.wo.cursorcolumn = false
vim.wo.cursorline = false
vim.cmd('VimadeBufDisable')
vim.cmd("startinsert!")
end,
on_close = function(term)
vim.cmd("quit!")
end
})

function _terminal_tig_toggle()
terminal_tig:toggle()
end

function _terminal_lazygit_toggle()
terminal_lazygit:toggle()
end

function _terminal_k9s_toggle()
terminal_k9s:toggle()
end
```

Used keymapping references

```lua
lua _terminal_horizontal_one_toggle()
lua _terminal_horizontal_two_toggle()
lua _terminal_horizontal_three_toggle()
lua _terminal_default_float_toggle()
lua _terminal_k9s_toggle()
lua _terminal_lazygit_toggle()
lua _terminal_tig_toggle
```

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。