akinsho/toggleterm.nvim

[question] Commands like `ToggleTermNewSplit`, `ToggleTermNewTab`.

mikew opened this issue · 1 comments

Really like this plugin! But I'm not a fan of how <count><open_mapping> works. Seems like when I'm in a terminal, it opens new splits, when I'm in a file, it opens a new terminal).

I'd much rather have my mapping show/hide/create an initial terminal, and explicit :ToggleTermNewSplit and :ToggleTermNewTab commands. When I go to open a new terminal / split, I don't want to have to know how many terminals I've opened already, I just want a new one.

I'm guessing this is something people can write themselves, but was wondering if anyone had any pointers.

@mikew I found a way to do so using the following ! I mapped it to a custom key binding, which is only available in terminal mode. It detects the current term(s) direction and simply toggle a new one using the same direction. Hope it helps !

function _G.open_next_terminal()
	local terminals = terms.get_all()
	local direction = terminals[1].direction
	Terminal:new({
		close_on_exit = true,
		direction = direction,
	}):toggle()
end

vim.api.nvim_set_keymap(
	"t",
	"<leader>tt",
	"<cmd>lua open_next_terminal()<CR>",
	{ noremap = true, silent = true }
)