From e94a81a0677f92df315b8e13789c3182a6d19217 Mon Sep 17 00:00:00 2001 From: Daniel Heras Quesada Date: Mon, 4 May 2026 23:34:19 +0200 Subject: [PATCH] feat: floating terminals by tj --- lua/config/remap.lua | 2 +- lua/plugins/floatingterminal.lua | 57 ++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 lua/plugins/floatingterminal.lua diff --git a/lua/config/remap.lua b/lua/config/remap.lua index 33c933c..13554e6 100644 --- a/lua/config/remap.lua +++ b/lua/config/remap.lua @@ -276,7 +276,7 @@ map("n", "uI", "InspectTree", { desc = "Inspect Tree" }) ---- Terminal ---- -map("n", "nz", "terminal", { desc = "Open new terminal" }) +map({ "n", "t" }, "tt", "Floaterminal", { desc = "Toggle terminal" }) -- Terminal Mappings map("t", "", "", { desc = "Enter Normal Mode" }) diff --git a/lua/plugins/floatingterminal.lua b/lua/plugins/floatingterminal.lua new file mode 100644 index 0000000..6933767 --- /dev/null +++ b/lua/plugins/floatingterminal.lua @@ -0,0 +1,57 @@ +vim.keymap.set("t", "", "") + +local state = { + floating = { + buf = -1, + win = -1, + }, +} + +local function create_floating_window(opts) + opts = opts or {} + local width = opts.width or math.floor(vim.o.columns * 0.8) + local height = opts.height or math.floor(vim.o.lines * 0.8) + + -- Calculate the position to center the window + local col = math.floor((vim.o.columns - width) / 2) + local row = math.floor((vim.o.lines - height) / 2) + + -- Create a buffer + local buf = nil + if vim.api.nvim_buf_is_valid(opts.buf) then + buf = opts.buf + else + buf = vim.api.nvim_create_buf(false, true) -- No file, scratch buffer + end + + -- Define window configuration + local win_config = { + relative = "editor", + width = width, + height = height, + col = col, + row = row, + style = "minimal", -- No borders or extra UI elements + border = "rounded", + } + + -- Create the floating window + local win = vim.api.nvim_open_win(buf, true, win_config) + + return { buf = buf, win = win } +end + +local toggle_terminal = function() + if not vim.api.nvim_win_is_valid(state.floating.win) then + state.floating = create_floating_window({ buf = state.floating.buf }) + if vim.bo[state.floating.buf].buftype ~= "terminal" then + vim.cmd.terminal() + end + else + vim.api.nvim_win_hide(state.floating.win) + end +end + +-- Example usage: +-- Create a floating window with default dimensions +vim.api.nvim_create_user_command("Floaterminal", toggle_terminal, {})