From ca3d4ef97be73b1b53af1647b04387a3c02a8566 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 17 Jun 2023 22:02:22 +0200 Subject: [PATCH] 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 . They can be invoked through their usual chords (tg and 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 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: tG and tP. --- nvim/.config/nvim/lua/core/mappings.lua | 7 +- .../nvim/lua/plugins/config/toggleterm.lua | 68 +++++++++++++------ 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/nvim/.config/nvim/lua/core/mappings.lua b/nvim/.config/nvim/lua/core/mappings.lua index 16e52da..10ba70b 100644 --- a/nvim/.config/nvim/lua/core/mappings.lua +++ b/nvim/.config/nvim/lua/core/mappings.lua @@ -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", "\\\\", [[]], { desc = "exit terminal mode" }) + -- remove search highlights by pressing space+/ map("n", "/", ":noh", { desc = "remove highlights" }) @@ -252,10 +255,12 @@ map("v", "nf", ":ZkMatch", { 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", "G", ":Lazygit") map("n", "tg", ":Lazygit") + map("n", "tG", ":Lazygit!") map("n", "tp", ":Pythonterm") + map("n", "tP", ":Pythonterm!") end prefix({ ["s"] = { name = "+set" } }) diff --git a/nvim/.config/nvim/lua/plugins/config/toggleterm.lua b/nvim/.config/nvim/lua/plugins/config/toggleterm.lua index b60b436..6bbd56d 100644 --- a/nvim/.config/nvim/lua/plugins/config/toggleterm.lua +++ b/nvim/.config/nvim/lua/plugins/config/toggleterm.lua @@ -1,6 +1,7 @@ require("toggleterm").setup({ open_mapping = [[=]], 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", "", 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()]]) -vim.cmd([[command! Pythonterm :lua _Pythonterm_toggle()]]) +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 } +)