-- keyboard.lua -- All keymaps centralized. Loaded after plugins so every mapping wins. local wk = require("which-key") local utils = require("utils") -- Helper: merge description into opts local function mdesc(opt, description) return vim.tbl_extend("force", opt, { desc = description }) end local map = function(mode, lhs, rhs, opts, desc) opts = opts and opts or {} opts = mdesc(opts, desc) vim.keymap.set(mode, lhs, rhs, opts) end -- ============================================================================= -- Which-key groups -- ============================================================================= wk.add({ { "b", group = "Buffer" }, { "c", group = "Code" }, { "d", group = "Debug" }, { "f", group = "Find" }, { "g", group = "Git" }, { "l", group = "LSP" }, { "s", group = "Search" }, { "t", group = "Test/Run" }, { "u", group = "UI Toggle" }, { "w", group = "Window/Workspace" }, }) -- ============================================================================= -- General -- ============================================================================= map({ "i", "v", "n", "s" }, "", function() vim.cmd([[w]]) end, {}, "Save file") map({ "i", "v", "n", "s" }, "ww", function() vim.cmd([[w]]) end, {}, "Save file") -- Better up/down on wrapped lines map("n", "j", "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true }) map("n", "k", "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = true }) -- ============================================================================= -- Window management -- ============================================================================= -- Navigation (normal + terminal) map({ "t", "i" }, "", [[h]]) map({ "t", "i" }, "", [[j]]) map({ "t", "i" }, "", [[k]]) map({ "t", "i" }, "", [[l]]) map("n", "", "h", {}, "Go to left window") map("n", "", "j", {}, "Go to lower window") map("n", "", "k", {}, "Go to upper window") map("n", "", "l", {}, "Go to right window") -- Resize map("n", "", "resize +2", {}, "Increase window height") map("n", "", "resize -2", {}, "Decrease window height") map("n", "", "vertical resize -2", {}, "Decrease window width") map("n", "", "vertical resize +2", {}, "Increase window width") -- Splits map("n", "wh", "s", {}, "Split window below") map("n", "wv", "v", {}, "Split window right") map("n", "wd", "c", {}, "Close window") map("n", "wo", "o", {}, "Close other windows") map("n", "w=", "=", {}, "Equalize windows") -- ============================================================================= -- Terminal -- ============================================================================= map({ "n", "t" }, "", function() vim.cmd.split({ "term://" .. vim.env.SHELL }) vim.cmd.resize({ 15 }) end, {}, "Toggle terminal") map("t", "", [[:bd!]], {}, "Close terminal") -- ============================================================================= -- Visual mode indent -- ============================================================================= map("v", "", ">gv", { noremap = true, silent = true }, "Indent selection") map("v", "", "bn", "bnext", {}, "Next buffer") map("n", "bp", "bprevious", {}, "Previous buffer") map("n", "bd", "bdelete", {}, "Delete buffer") map("n", "bD", "bdelete!", {}, "Delete buffer (force)") -- ============================================================================= -- Find (Telescope) -- ============================================================================= local builtin = require("telescope.builtin") map("n", "ff", builtin.find_files, {}, "Find files") map("n", "fg", builtin.live_grep, {}, "Live grep") map("n", "fb", builtin.buffers, {}, "Buffers") map("n", "fh", builtin.help_tags, {}, "Help tags") map("n", "fr", builtin.oldfiles, {}, "Recent files") map("n", "fB", function() require("telescope").extensions.file_browser.file_browser() end, {}, "File browser") -- ============================================================================= -- Git -- ============================================================================= map("n", "gg", "Neogit", {}, "Neogit") map("n", "gd", "DiffviewOpen", {}, "Diffview open") map("n", "gD", "DiffviewFileHistory %", {}, "Diffview file history") map("n", "gq", "DiffviewClose", {}, "Diffview close") -- ============================================================================= -- Search -- ============================================================================= map("n", "S", function() require("spectre").toggle() end, {}, "Toggle Spectre") map("n", "sw", function() require("spectre").open_visual({ select_word = true }) end, {}, "Search current word") map("v", "sw", function() require("spectre").open_visual() end, {}, "Search current word") map("n", "sf", function() require("spectre").open_file_search({ select_word = true }) end, {}, "Search in current file") -- ============================================================================= -- Todo Comments -- ============================================================================= map("n", "]t", function() require("todo-comments").jump_next() end, {}, "Next todo comment") map("n", "[t", function() require("todo-comments").jump_prev() end, {}, "Previous todo comment") map("n", "st", "TodoTelescope", {}, "Search todos") map("n", "sT", "TodoLocList", {}, "Search todos (LocList)") -- ============================================================================= -- Test (Neotest) -- ============================================================================= local neotest = require("neotest") map("n", "tf", function() neotest.run.run(vim.fn.expand("%")) end, {}, "Run File") map("n", "tt", function() neotest.run.run() end, {}, "Run Nearest") map("n", "tw", function() neotest.run.run(vim.loop.cwd()) end, {}, "Run Workspace") map("n", "tr", function() neotest.output.open({ short = true, auto_close = true }) end, {}, "Results (short)") map("n", "tR", function() neotest.output.open({ enter = true }) end, {}, "Results (full)") -- ============================================================================= -- Run / Overseer -- ============================================================================= map("n", "te", function() require("overseer.template.run_watch_provider").pick_executable_and_run() end, {}, "Run & Watch executable (Telescope)") map("n", "to", "OverseerToggle", {}, "Toggle Overseer") map("n", "ts", "OverseerRun", {}, "Run Overseer") -- ============================================================================= -- Debug (nvim-dap) -- ============================================================================= local dap = require("dap") local dapui = require("dapui") local pb = require("persistent-breakpoints.api") map("n", "db", pb.toggle_breakpoint, {}, "Toggle breakpoint") map("n", "dB", function() pb.set_conditional_breakpoint() end, {}, "Conditional breakpoint") map("n", "dc", dap.continue, {}, "Continue") map("n", "dO", dap.step_over, {}, "Step over") map("n", "di", dap.step_into, {}, "Step into") map("n", "do", dap.step_out, {}, "Step out") map("n", "dr", dap.run_to_cursor, {}, "Run to cursor") map("n", "dd", dapui.toggle, {}, "Toggle DAP UI") map("n", "dq", function() dap.terminate() end, {}, "Terminate debug") -- ============================================================================= -- LSP (buffer-local via LspAttach) -- ============================================================================= vim.api.nvim_create_autocmd("LspAttach", { group = vim.api.nvim_create_augroup("UserLspKeymaps", { clear = true }), callback = function(ev) local bufnr = ev.buf local client = vim.lsp.get_client_by_id(ev.data.client_id) if not client then return end local opts = { buffer = bufnr, silent = true } local autocmd = vim.api.nvim_create_autocmd -- Hover & Signature if client.server_capabilities.hoverProvider then map("n", "K", vim.lsp.buf.hover, opts, "Hover") end if client.server_capabilities.signatureHelpProvider then map("n", "k", vim.lsp.buf.signature_help, opts, "Signature help") end -- Goto if client.server_capabilities.declarationProvider then map("n", "gD", vim.lsp.buf.declaration, opts, "Go to declaration") end if client.server_capabilities.definitionProvider then map("n", "gd", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga goto_definition]]) else vim.lsp.buf.definition() end end, opts, "Go to definition") if utils.has("lspsaga.nvim") then map("n", "lp", function() vim.cmd([[Lspsaga peek_definition]]) end, opts, "Peek definition") end end if client.server_capabilities.typeDefinitionProvider then map("n", "gt", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga goto_type_definition]]) else vim.lsp.buf.type_definition() end end, opts, "Go to type definition") if utils.has("lspsaga.nvim") then map("n", "lP", function() vim.cmd([[Lspsaga peek_type_definition]]) end, opts, "Peek type definition") end end if client.server_capabilities.implementationProvider then map("n", "gi", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga finder imp+def]]) else vim.lsp.buf.implementation() end end, opts, "Go to implementation") end if client.server_capabilities.referencesProvider then map("n", "gr", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga finder ref]]) else vim.lsp.buf.references({}) end end, opts, "Go to references") end -- Document Highlight (references under cursor) if client.server_capabilities.documentHighlightProvider then vim.api.nvim_set_hl(0, "LspReferenceRead", { link = "DiffText" }) vim.api.nvim_set_hl(0, "LspReferenceText", { link = "IncSearch" }) vim.api.nvim_set_hl(0, "LspReferenceWrite", { link = "WildMenu" }) local doc_highlight = vim.api.nvim_create_augroup("lsp_document_highlight", { clear = true }) local enable_highlight = function() autocmd({ "CursorHold", "CursorHoldI" }, { group = doc_highlight, buffer = bufnr, callback = vim.lsp.buf.document_highlight, }) autocmd("CursorMoved", { group = doc_highlight, buffer = bufnr, callback = vim.lsp.buf.clear_references, }) end local disable_highlight = function() vim.lsp.buf.clear_references() vim.api.nvim_clear_autocmds({ buffer = bufnr, group = doc_highlight }) end map("n", "uh", function() utils.toggle("highlight", {}, { enable_highlight, disable_highlight }) local fn = vim.b[vim.fn.bufnr()]["highlight"] if fn then fn() end end, opts, "Toggle document highlight") end -- Symbols if client.server_capabilities.documentSymbolProvider then map("n", "ls", vim.lsp.buf.document_symbol, opts, "Document symbols") end if client.server_capabilities.workspaceSymbolProvider then map("n", "lS", vim.lsp.buf.workspace_symbol, opts, "Workspace symbols") end -- Code Action & Rename if client.server_capabilities.codeActionProvider then map({ "n", "v" }, "ca", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga code_action]]) else vim.lsp.buf.code_action() end end, opts, "Code action") end if client.server_capabilities.renameProvider then map("n", "cr", vim.lsp.buf.rename, opts, "Rename") if utils.has("lspsaga.nvim") then map("n", "cR", function() vim.cmd([[Lspsaga project_replace]]) end, opts, "Rename workspace") end end -- Calls if client.server_capabilities.callHierarchyProvider then map("n", "ci", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga incoming_calls]]) else vim.lsp.buf.incoming_calls() end end, opts, "Incoming calls") map("n", "co", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga outgoing_calls]]) else vim.lsp.buf.outgoing_calls() end end, opts, "Outgoing calls") end -- Formatting if client.server_capabilities.documentFormattingProvider then map("n", "cf", function() vim.lsp.buf.format({ async = true }) end, opts, "Format buffer") end if client.server_capabilities.documentRangeFormattingProvider then map("v", "cf", function() vim.lsp.buf.format({ async = true }) end, opts, "Format selection") end -- Diagnostics map("n", "[d", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga diagnostic_jump_prev]]) else vim.diagnostic.jump({ count = -1, float = true }) end end, opts, "Previous diagnostic") map("n", "]d", function() if utils.has("lspsaga.nvim") then vim.cmd([[Lspsaga diagnostic_jump_next]]) else vim.diagnostic.jump({ count = 1, float = true }) end end, opts, "Next diagnostic") map("n", "lf", vim.diagnostic.open_float, opts, "Line diagnostic") map("n", "lq", vim.diagnostic.setloclist, opts, "Set loclist") -- Lspsaga extras if utils.has("lspsaga.nvim") then map("n", "lw", function() vim.cmd([[Lspsaga show_workspace_diagnostics]]) end, opts, "Workspace diagnostics") map("n", "lb", function() vim.cmd([[Lspsaga show_buf_diagnostics]]) end, opts, "Buffer diagnostics") map("n", "lo", function() vim.cmd([[Lspsaga outline]]) end, opts, "Outline") end -- Workspace folders map("n", "wf", function() vim.print(vim.lsp.buf.list_workspace_folders()) end, opts, "List workspace folders") map("n", "wr", function() local clients = vim.lsp.get_clients() if #clients > 0 then local lsp = vim.lsp.status() if lsp ~= "" then print(lsp) return end end print("No LSP status available") end, opts, "LSP ready check") end, }) -- ============================================================================= -- UI Toggles (global) -- ============================================================================= map("n", "us", function() utils.toggle("enable_semantic_tokens", { global = true }, nil) local enabled = vim.g["enable_semantic_tokens"] for _, client in ipairs(vim.lsp.get_clients()) do if client.server_capabilities.semanticTokensProvider then for _, buf in ipairs(vim.api.nvim_list_bufs()) do if vim.api.nvim_buf_is_loaded(buf) then if enabled then vim.lsp.semantic_tokens.start(buf, client.id) else vim.lsp.semantic_tokens.stop(buf, client.id) end end end end end end, {}, "Toggle semantic tokens") vim.api.nvim_create_autocmd("BufWritePre", { group = vim.api.nvim_create_augroup("LspFormat", { clear = true }), callback = function(args) if vim.g["autoformat"] then vim.lsp.buf.format({ async = true, bufnr = args.buf }) end end, }) -- ============================================================================= -- LSP Document Highlight jumps (lsp-document-highlight.nvim) -- ============================================================================= map("n", "[[", function() require("lsp-document-highlight").jump(-vim.v.count1, true) end, {}, "Previous reference") map("n", "]]", function() require("lsp-document-highlight").jump(vim.v.count1, true) end, {}, "Next reference")