-
-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question :: (Re)-focus on open terminal window using it's count number #42
Comments
Hi @lcrockett 👋🏾 , so you've actually stumbled upon a bug that I haven't really been tracking anywhere. In the initial implementation of this plugin it was designed so this would work i.e. It's on my list though of things to fix 👍🏾 |
Cheers ! Let me know if you need more information or a test of the fix once available. |
@lcrockett I actually just came back to this after a while and realised I'd read the exact request incorrectly the first time. I thought the issue was that specifying a count wasn't opening the correct terminal. Which was an issue i.e. The specific issue here though seems to be about going to an unfocused but open toggle term, which tbh is an understandable use case but I think in hindsight one I personally will outsource to someone else. This isn't really something I need/would use so not particularly keen on spending time on it, not that it's a bad idea or anything. Happy to help anyone who wants to take it on re. reviewing a PR or coming up with a solution. |
@akinsho Cheers on the understandable feedback. Trying to focus back on an unfocused but previously opened and currently open toggle term is indeed what i'm after. |
I realized this can be achieved with the vim.g.self_plugin_toggleterm_horizontal_one_window_number = 0
vim.g.self_plugin_toggleterm_horizontal_two_window_number = 0
vim.g.self_plugin_toggleterm_horizontal_three_window_number = 0
local terminal_horizontal_one = require('toggleterm.terminal').Terminal:new({
count = 11,
direction = 'horizontal',
on_open = function(term)
vim.g.self_plugin_toggleterm_horizontal_one_window_number = vim.api.nvim_get_current_win()
vim.api.nvim_buf_set_keymap(term.bufnr, 'n', 'q', '<cmd>close<cr>', { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(term.bufnr, 't', '<c-b>', '<c-\\><c-n><cmd>close<cr><c-w><c-p>', { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(term.bufnr, 't', '<c-k>', '<c-\\><c-n><c-w><c-p>', { noremap = true, silent = true })
vim.wo.cursorcolumn = false
vim.wo.cursorline = false
vim.cmd('VimadeBufDisable')
vim.cmd("startinsert!")
end,
---@diagnostic disable-next-line
on_close = function(term)
vim.g.self_plugin_toggleterm_horizontal_one_window_number = 0
vim.cmd("quit!")
end
})
local terminal_horizontal_two = require('toggleterm.terminal').Terminal:new({
count = 12,
direction = 'horizontal',
on_open = function(term)
vim.g.self_plugin_toggleterm_horizontal_two_window_number = vim.api.nvim_get_current_win()
vim.api.nvim_buf_set_keymap(term.bufnr, 'n', 'q', '<cmd>close<cr>', { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(term.bufnr, 't', '<c-b>', '<c-\\><c-n><cmd>close<cr><c-w><c-p>', { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(term.bufnr, 't', '<c-k>', '<c-\\><c-n><c-w><c-p>', { noremap = true, silent = true })
vim.wo.cursorcolumn = false
vim.wo.cursorline = false
vim.cmd('VimadeBufDisable')
vim.cmd("startinsert!")
end,
---@diagnostic disable-next-line
on_close = function(term)
vim.g.self_plugin_toggleterm_horizontal_two_window_number = 0
vim.cmd("quit!")
end
})
local terminal_horizontal_three = require('toggleterm.terminal').Terminal:new({
count = 13,
direction = 'horizontal',
on_open = function(term)
vim.g.self_plugin_toggleterm_horizontal_three_window_number = vim.api.nvim_get_current_win()
vim.api.nvim_buf_set_keymap(term.bufnr, 'n', 'q', '<cmd>close<cr>', { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(term.bufnr, 't', '<c-b>', '<c-\\><c-n><cmd>close<cr><c-w><c-p>', { noremap = true, silent = true })
vim.api.nvim_buf_set_keymap(term.bufnr, 't', '<c-k>', '<c-\\><c-n><c-w><c-p>', { noremap = true, silent = true })
vim.wo.cursorcolumn = false
vim.wo.cursorline = false
vim.cmd('VimadeBufDisable')
vim.cmd("startinsert!")
end,
---@diagnostic disable-next-line
on_close = function(term)
vim.g.self_plugin_toggleterm_horizontal_three_window_number = 0
vim.cmd("quit!")
end
})
function _G.terminal_horizontal_one_toggle()
if vim.g.self_plugin_toggleterm_horizontal_one_window_number > 0 then
if vim.tbl_contains(vim.api.nvim_list_wins(), vim.g.self_plugin_toggleterm_horizontal_one_window_number) then
vim.api.nvim_set_current_win(vim.g.self_plugin_toggleterm_horizontal_one_window_number)
else
vim.g.self_plugin_toggleterm_horizontal_one_window_number = 0
terminal_horizontal_one:toggle()
end
else
terminal_horizontal_one:toggle()
end
end
function _G.terminal_horizontal_two_toggle()
if vim.g.self_plugin_toggleterm_horizontal_two_window_number > 0 then
if vim.tbl_contains(vim.api.nvim_list_wins(), vim.g.self_plugin_toggleterm_horizontal_two_window_number) then
vim.api.nvim_set_current_win(vim.g.self_plugin_toggleterm_horizontal_two_window_number)
else
vim.g.self_plugin_toggleterm_horizontal_two_window_number = 0
terminal_horizontal_two:toggle()
end
else
terminal_horizontal_two:toggle()
end
end
function _G.terminal_horizontal_three_toggle()
if vim.g.self_plugin_toggleterm_horizontal_three_window_number > 0 then
if vim.tbl_contains(vim.api.nvim_list_wins(), vim.g.self_plugin_toggleterm_horizontal_three_window_number) then
vim.api.nvim_set_current_win(vim.g.self_plugin_toggleterm_horizontal_three_window_number)
else
vim.g.self_plugin_toggleterm_horizontal_three_window_number = 0
terminal_horizontal_three:toggle()
end
else
terminal_horizontal_three:toggle()
end
end One can use a Lua call to one of the defined functions to pop open a terminal window, for instance |
@lcrockett _G.focus_toggleterm = function(count)
local terms = require("toggleterm.terminal"):get_all()
if vim.tbl_isempty(terms) then return end
local pwin = _G.toggleterm_last_editor_winnr
if count == 0 or not count then count = 1 end
local term_focused = vim.tbl_contains((function(terms)
return vim.tbl_map(function(term)
return term.window
end, terms)
end)(terms), vim.api.nvim_get_current_win())
if term_focused then
vim.api.nvim_set_current_win(pwin)
else
_G.toggleterm_last_editor_winnr = vim.api.nvim_get_current_win()
local term = terms[count]
if term:is_open() then
local start_in_insert = require"toggleterm.config".get("start_in_insert")
vim.api.nvim_set_current_win(term.window)
if start_in_insert then vim.cmd("startinsert") end
end
end
end
_G.toggleterm_wrap = function(arg, count)
_G.toggleterm_last_editor_winnr = vim.api.nvim_get_current_win()
require"toggleterm".toggle_command(arg, count)
end
vim.cmd("command! -count FocusTerm lua focus_toggleterm(<count>)")
vim.cmd("command! -count -nargs=* ToggleTerm lua toggleterm_wrap(<q-args>, <count>)")
local opts = {noremap = true}
function _G.set_terminal_keymaps()
vim.api.nvim_buf_set_keymap(0, 't', '<A-1>', [[<cmd>lua focus_toggleterm(1)<cr>]], opts)
vim.api.nvim_buf_set_keymap(0, 't', '<A-2>', [[<cmd>lua focus_toggleterm(2)<cr>]], opts)
vim.api.nvim_buf_set_keymap(0, 't', '<A-3>', [[<cmd>lua focus_toggleterm(3)<cr>]], opts)
end
vim.api.nvim_set_keymap('n', '<A-1>', [[<cmd>lua focus_toggleterm(1)<cr>]], opts)
vim.api.nvim_set_keymap('n', '<A-2>', [[<cmd>lua focus_toggleterm(2)<cr>]], opts)
vim.api.nvim_set_keymap('n', '<A-3>', [[<cmd>lua focus_toggleterm(3)<cr>]], opts)
-- if you only want these mappings for toggle term use term:https://*toggleterm#* instead
vim.cmd('autocmd! TermOpen term:https://* lua set_terminal_keymaps()')
Also, I had to put this line I couldn't find a way to achieve this without modifying the |
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).
: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 withFailed 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
Used keymapping references
The text was updated successfully, but these errors were encountered: