nvim: Make utility terminals dockable and hidable

The utility terminals (lazygit and python repl for now) can now be hidden
even from terminal insert mode (i.e. when interacting with them) with
<C-\>. They can be invoked through their usual chords (<leader>tg and
<leader>tp respectively) again and will pick right up where you left off.

Insert mode in terminals can also be left slightly easier should it
be needed: Instead of the <C-\><C-n> chord you can use j\.

Lastly, the utility terminals can be started in a vertically docked mode
instead of floating. This is done by adding a bang to their commands,
`Lazygit!` and `Pythonterm!`, or using capital versions of their
mappings: <leader>tG and <leader>tP.
This commit is contained in:
Marty Oehme 2023-06-17 22:02:22 +02:00
parent b9de8b3914
commit ca3d4ef97b
Signed by: Marty
GPG Key ID: EDBF2ED917B2EF6A
2 changed files with 55 additions and 20 deletions

View File

@ -96,6 +96,9 @@ if vim.g.maplocalleader == "," then
vim.keymap.del("", ",,", { silent = true })
end
-- get out of terminal mode a little easier by double tapping backslash
map("t", "\\\\", [[<C-\><C-n>]], { desc = "exit terminal mode" })
-- remove search highlights by pressing space+/
map("n", "<leader>/", ":noh<cr>", { desc = "remove highlights" })
@ -252,10 +255,12 @@ map("v", "<localleader>nf", ":ZkMatch<cr>", { desc = "find note from selection"
-- PLUGIN: toggleterm.nvim
-- create a lazygit or python window, set up in toggleterm settings
-- TODO create ability to go into python environment when in poetry venv and/or euporie/jupyter notebook
if require("util").is_available("nvim-toggleterm.lua") then
if is_available("nvim-toggleterm.lua") then
map("n", "<leader>G", ":Lazygit<cr>")
map("n", "<leader>tg", ":Lazygit<cr>")
map("n", "<leader>tG", ":Lazygit!<cr>")
map("n", "<leader>tp", ":Pythonterm<cr>")
map("n", "<leader>tP", ":Pythonterm!<cr>")
end
prefix({ ["<localleader>s"] = { name = "+set" } })

View File

@ -1,6 +1,7 @@
require("toggleterm").setup({
open_mapping = [[<leader>=]],
insert_mappings = false, -- don't map the key in insert mode
terminal_mappings = false,
})
local Terminal = require("toggleterm.terminal").Terminal
@ -15,15 +16,10 @@ if require("util").is_available("mini.nvim") then
})
end
-- create a lazygit window with the lazygit command
local lazygit = Terminal:new({
cmd = "lazygit",
hidden = true,
direction = "float",
float_opts = { border = "curved" },
})
function _Lazygit_toggle()
lazygit:toggle()
local function custom_term_set_toggle_key(term)
vim.keymap.set("t", "<C-\\>", function()
term:toggle()
end, { silent = true, buffer = true })
end
-- create python window
@ -41,15 +37,49 @@ local function get_python_cmd()
return "python"
end
end
local pythonterm = Terminal:new({
cmd = get_python_cmd(),
hidden = true,
direction = "float",
float_opts = { border = "curved" },
})
function _Pythonterm_toggle()
pythonterm:toggle()
local terms = {
lazygit = Terminal:new({
cmd = "lazygit",
hidden = true,
direction = "float",
float_opts = { border = "curved" },
on_open = custom_term_set_toggle_key,
}),
python = Terminal:new({
cmd = get_python_cmd(),
hidden = true,
direction = "float",
float_opts = { border = "curved" },
on_open = custom_term_set_toggle_key,
}),
}
-- create a lazygit window with the lazygit command
local function toggle_custom_term(term, bang, vertsize)
vertsize = vertsize or vim.o.columns * 0.4
if not bang then
term.direction = "float"
term:toggle()
else
term.direction = "vertical"
term:resize(vertsize)
term:toggle()
end
end
vim.cmd([[command! Lazygit :lua _Lazygit_toggle()<cr>]])
vim.cmd([[command! Pythonterm :lua _Pythonterm_toggle()<cr>]])
local function _Pythonterm_toggle(opts)
toggle_custom_term(terms.python, opts.bang)
end
local function _Lazygit_toggle(opts)
toggle_custom_term(terms.lazygit, opts.bang, vim.o.columns * 0.6)
end
vim.api.nvim_create_user_command(
"Lazygit",
_Lazygit_toggle,
{ desc = "Toggle floating Lazygit terminal", bang = true }
)
vim.api.nvim_create_user_command(
"Pythonterm",
_Pythonterm_toggle,
{ desc = "Toggle floating Python terminal", bang = true }
)