Browse Source

Updated nvim terminal management to use terminal 99C-/ for floating terminal (not hijacking C-i)

master
chodak166 1 month ago
parent
commit
e01a1f7b34
  1. 14
      .config/nvim/lua/config/keymaps.lua
  2. 27
      .config/nvim/lua/config/terminal.lua
  3. 2
      .config/nvim/lua/plugins/terminal.lua

14
.config/nvim/lua/config/keymaps.lua

@ -1,3 +1,17 @@
-- Keymaps are automatically loaded on the VeryLazy event
-- Default keymaps that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/keymaps.lua
-- Add any additional keymaps here
-- Override LazyVim's <C-/> / <C-_>: count 99 opens the floating terminal,
-- everything else stays native (bottom terminal at project root).
-- Both keys are mapped because terminals differ in which code Ctrl+/ sends.
local function term_open()
if vim.v.count == 99 then
require("config.terminal").open_float()
else
Snacks.terminal.focus(nil, { cwd = LazyVim.root() })
end
end
vim.keymap.set({ "n", "t" }, "<C-/>", term_open, { desc = "Terminal (Root Dir) | 99<C-/> = float" })
vim.keymap.set({ "n", "t" }, "<C-_>", term_open, { desc = "Terminal (Root Dir) | 99<C-/> = float" })

27
.config/nvim/lua/config/terminal.lua

@ -5,20 +5,19 @@
-- count, e.g. 2<C-/> opens a *separate* terminal #2)
-- <leader>fT terminal in the current working directory
--
-- This module adds:
-- <Tab> open/focus a *dedicated* floating terminal (close it with <C-/>)
-- <leader>ft a picker that lists every live terminal so any contextual
-- terminal can be focused or restored even after its window was
-- closed/hidden.
-- This module adds (actual key bindings are declared in lua/plugins/terminal.lua):
-- open_float() open/focus a *dedicated* floating terminal (close it with <C-/>)
-- picker() list every live terminal so any contextual terminal can be
-- focused or restored even after its window was closed/hidden.
local M = {}
-- The <Tab> floating terminal. Tracked as an upvalue so <Tab> always re-focuses
-- The floating terminal. Tracked as an upvalue so the same key always re-focuses
-- the *same* float regardless of which directory the editor is in.
---@type snacks.win?
local float
--- Open or focus the <Tab> floating terminal, or hide it if already focused.
-- NOTE: currently unused; <Tab> calls open_float() instead so shell
--- Open or focus the floating terminal, or hide it if already focused.
-- NOTE: currently unused; the binding calls open_float() instead so shell
-- tab-completion keeps working while the terminal is open. Kept around for a
-- future explicit open/close toggle.
function M.toggle_float()
@ -35,17 +34,17 @@ function M.toggle_float()
-- First open: a high fixed count guarantees this terminal never collides with
-- the count-keyed bottom terminals (1, 2, 3 ...), so it is always the *same*
-- float for <Tab> no matter the context.
-- float for <C-Tab> no matter the context.
float = Snacks.terminal(nil, {
count = 99,
win = { position = "float", border = "rounded" },
})
-- Close it with a single keystroke from inside the shell: <Tab> hides the
-- Close it with a single keystroke from inside the shell: hides the
-- window (buffer-local, so it only affects this floating terminal and never
-- the bottom split, where shell tab-completion must keep working).
if float and float.buf then
vim.keymap.set("t", "<Tab>", function()
vim.keymap.set("t", "<leader>tt", function()
if float and float:win_valid() then
float:hide()
end
@ -53,8 +52,8 @@ function M.toggle_float()
end
end
--- Open or focus the <Tab> floating terminal. This never hides the window, so
--- once the float is open <Tab> falls through to the shell and stays available
--- Open or focus the floating terminal. This never hides the window, so
--- once the float is open the key falls through to the shell and stays available
--- for tab-completion. Close the floating terminal with <C-/> while it is
--- visible (handled by Snacks' native terminal keymap).
function M.open_float()
@ -71,7 +70,7 @@ function M.open_float()
-- First open: a high fixed count guarantees this terminal never collides with
-- the count-keyed bottom terminals (1, 2, 3 ...), so it is always the *same*
-- float for <Tab> no matter the context.
-- float for <C-Tab> no matter the context.
float = Snacks.terminal(nil, {
count = 99,
win = { position = "float", border = "rounded" },

2
.config/nvim/lua/plugins/terminal.lua

@ -5,7 +5,7 @@ return {
"folke/snacks.nvim",
keys = {
{
"<Tab>",
"<leader>tt",
function()
require("config.terminal").open_float()
end,

Loading…
Cancel
Save