From 73fd0a2f26545fb4e0a6a79a91946ef991a23879 Mon Sep 17 00:00:00 2001 From: "RingOfStorms (Joshua Bell)" Date: Tue, 18 Feb 2025 19:33:27 -0600 Subject: [PATCH] better tab behavior --- lua/keymaps.lua | 5 -- lua/tools/tabs.lua | 148 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 5 deletions(-) create mode 100644 lua/tools/tabs.lua diff --git a/lua/keymaps.lua b/lua/keymaps.lua index 91ac83e..2f6d5a0 100644 --- a/lua/keymaps.lua +++ b/lua/keymaps.lua @@ -109,11 +109,6 @@ U.keymaps({ { "", "", mode = { "i", "c" }, desc = "Movements in insert/command mode" }, { "", "", mode = { "i", "c" }, desc = "Movements in insert/command mode" }, - -- Tabs - { "tn", "tabnew", desc = "Create new tab", mode = nvx }, - { "tq", "tabclose", desc = "Close current tab", mode = nvx }, - { "H", "tabprevious", desc = "Move to previous tab" }, - { "L", "tabnext", desc = "Move to next tab" }, -- LSP/IDE/ { diff --git a/lua/tools/tabs.lua b/lua/tools/tabs.lua new file mode 100644 index 0000000..ddb5802 --- /dev/null +++ b/lua/tools/tabs.lua @@ -0,0 +1,148 @@ +local function reindex_tabs() + local tabs = vim.fn.gettabinfo() + for i, tab in ipairs(tabs) do + vim.cmd(string.format("silent! %dtabn", tab.tabnr)) + if i ~= tab.tabnr then + vim.cmd(string.format("silent! tabmove %d", i - 1)) + end + end +end + +local function switch_to_tab(tab_number) + local tabs = vim.fn.gettabinfo() + if tab_number <= #tabs then + vim.cmd(string.format("%dtabn", tab_number)) + end +end + +local function close_current_tab() + vim.cmd("tabclose") + reindex_tabs() +end + +-- Create autocommand to reindex tabs when a tab is closed +vim.api.nvim_create_autocmd("TabClosed", { + callback = function() + vim.schedule(reindex_tabs) + end, +}) + +-- Show custom tab name +-- vim.o.showtabline = 2 +vim.o.tabline = "%!v:lua.CustomTabLine()" +-- vim.opt.tabline = '%!t' + +function _G.CustomTabLine() + local tabline = "" + for i = 1, vim.fn.tabpagenr("$") do + -- Determine the active state + local hl = i == vim.fn.tabpagenr() and "%#TabLineSel#" or "%#TabLine#" + + -- Get the tab name + local tab_name = "Tab" + + -- Check if a custom name exists + if vim.g.tab_names and vim.g.tab_names[i] then + tab_name = vim.g.tab_names[i] + else + -- If no custom name, try to get buffer name + local winnr = vim.fn.tabpagewinnr(i) + local buflist = vim.fn.tabpagebuflist(i) + local bufnr = buflist[winnr] + local filename = vim.fn.bufname(bufnr) + + -- Use filename or default + if filename ~= "" then + tab_name = vim.fn.fnamemodify(filename, ":t") + end + end + + -- Create tab label + tabline = tabline .. hl .. "%" .. i .. "T " .. i .. ":" .. tab_name .. " %T" + end + + -- Fill the rest of the tabline + tabline = tabline .. "%#TabLineFill#%T" + return tabline +end + +local nvx = { "n", "v", "x" } +U.keymaps({ + -- Tabs + { "tc", "tabnew", desc = "Create new tab", mode = nvx }, + { "tx", close_current_tab, desc = "Close current tab", mode = nvx }, + + -- Switch to tabs 1-9 + { + "t1", + function() + switch_to_tab(1) + end, + desc = "Switch to tab 1", + mode = nvx, + }, + { + "t2", + function() + switch_to_tab(2) + end, + desc = "Switch to tab 2", + mode = nvx, + }, + { + "t3", + function() + switch_to_tab(3) + end, + desc = "Switch to tab 3", + mode = nvx, + }, + { + "t4", + function() + switch_to_tab(4) + end, + desc = "Switch to tab 4", + mode = nvx, + }, + { + "t5", + function() + switch_to_tab(5) + end, + desc = "Switch to tab 5", + mode = nvx, + }, + { + "t6", + function() + switch_to_tab(6) + end, + desc = "Switch to tab 6", + mode = nvx, + }, + { + "t7", + function() + switch_to_tab(7) + end, + desc = "Switch to tab 7", + mode = nvx, + }, + { + "t8", + function() + switch_to_tab(8) + end, + desc = "Switch to tab 8", + mode = nvx, + }, + { + "t9", + function() + switch_to_tab(9) + end, + desc = "Switch to tab 9", + mode = nvx, + }, +})