vim.g.mapleader = " " local map = vim.keymap.set ---- Bufferline ---- map("n", "bg", "BufferLinePick") map("n", "bpx", "BufferLinePickClose") map("n", "box", "BufferLineCloseOthers") map("n", "", "BufferLineCycleNext") map("n", "", "BufferLineCyclePrev") map("n", "bsd", "BufferLineSortByDirectory") map("n", "bse", "BufferLineSortByExtension") map("n", "bsr", "BufferLineSortByRelativeDirectory") map("n", "bst", "BufferLineSortByTabs") -- buffers map("n", "", "BufferLineMovePrev", { desc = "Move buffer to left" }) map("n", "", "BufferLineMoveNext", { desc = "Move buffer to right" }) map("n", "[b", "bprevious", { desc = "Prev Buffer" }) map("n", "]b", "bnext", { desc = "Next Buffer" }) map("n", "bd", function() Snacks.bufdelete() end, { desc = "Delete Buffer" }) map("n", "x", function() Snacks.bufdelete() end, { desc = "Delete Buffer" }) -- tabpages map("n", "]", ":+tabnext", { desc = "Goes to the next tabpage" }) map("n", "[", ":-tabnext", { desc = "Goes to the previous tabpage" }) map("n", "nt", ":tabnew", { desc = "Create new tabpage" }) --------------------------------------------------------------------------- -- Treesitter context map("n", "tc", "TSContextToggle", { desc = "Toggle treesitter context" }) -- Regex search and replace map("n", "S", 'lua require("spectre").toggle()', { desc = "Toggle Spectre", }) map("n", "sw", 'lua require("spectre").open_visual({select_word=true})', { desc = "Search current word", }) map("v", "sw", 'lua require("spectre").open_visual()', { desc = "Search current word", }) map("n", "sp", 'lua require("spectre").open_file_search({select_word=true})', { desc = "Search on current file", }) -- Blueprints map("n", "ct", "lua require('blueprints').createFromTemplateTelescope()", { desc = "Nvim blueprints" }) ---- Git ---- map("n", "gb", "Gitsigns blame") map("n", "gd", "DiffviewOpen") map("n", "gx", "DiffviewClose") map("n", "K", "Gitsigns blame_line") map("n", "gh", "Gitsigns preview_hunk_inline") map("n", "gtb", "Gitsigns toggle_current_line_blame") map("n", "gtd", "Gitsigns toggle_deleted") map("n", "gc", "Telescope git_commits ", { desc = "Git commits" }) map("n", "gs", "Telescope git_status ", { desc = "Git status" }) -- lazygit map("n", "gg", function() Snacks.lazygit({ cwd = LazyVim.root.git() }) end, { desc = "Lazygit (Root Dir)" }) map("n", "gG", function() Snacks.lazygit() end, { desc = "Lazygit (cwd)" }) map("n", "gB", function() Snacks.lazygit.browse() end, { desc = "Git Browse" }) map("n", "gf", function() local git_path = vim.api.nvim_buf_get_name(0) Snacks.lazygit({ args = { "-f", vim.trim(git_path) } }) end, { desc = "Lazygit Current File History" }) map("n", "gl", function() Snacks.lazygit({ args = { "log" }, cwd = LazyVim.root.git() }) end, { desc = "Lazygit Log" }) map("n", "gL", function() Snacks.lazygit({ args = { "log" } }) end, { desc = "Lazygit Log (cwd)" }) --------------------------------------------------------------------------- -- Neotree map("n", "", "Neotree toggle", { desc = "Toggle Neotree open" }) map("n", "e", "Neotree focus", { desc = "Focus neotree" }) -- LSP map("n", "gD", function() vim.lsp.buf.declaration() end, { desc = "LSP declaration" }) map("n", "gd", function() vim.lsp.buf.definition() end, { desc = "LSP definition" }) map("n", "gr", function() vim.lsp.buf.references() end, { desc = "LSP references" }) map("n", "K", function() vim.lsp.buf.hover() end, { desc = "LSP hover" }) map("n", "gi", function() vim.lsp.buf.implementation() end, { desc = "LSP implementation" }) map("n", "ls", function() vim.lsp.buf.signature_help() end, { desc = "LSP signature help" }) map("n", "lf", function() vim.diagnostic.open_float({ border = "rounded" }) end, { desc = "Floating diagnostics" }) map("n", "D", function() vim.lsp.buf.type_definition() end, { desc = "LSP type definition" }) map("n", "ca", function() vim.lsp.buf.code_action() end, { desc = "LSP code actions" }) map("n", "[d", function() vim.diagnostic.goto_prev({ float = { border = "rounded" } }) end, { desc = "Goto prev diagnostic" }) map("n", "]d", function() vim.diagnostic.goto_next({ float = { border = "rounded" } }) end, { desc = "Goto next diagnostic" }) map("v", "ca", function() vim.lsp.buf.code_action() end, { desc = "LSP code action" }) map("n", "r", function() -- when rename opens the prompt, this autocommand will trigger -- it will "press" CTRL-F to enter the command-line window `:h cmdwin` -- in this window I can use normal mode keybindings local cmdId cmdId = vim.api.nvim_create_autocmd({ "CmdlineEnter" }, { callback = function() local key = vim.api.nvim_replace_termcodes("", true, false, true) vim.api.nvim_feedkeys(key, "c", false) vim.api.nvim_feedkeys("0", "n", false) -- autocmd was triggered and so we can remove the ID and return true to delete the autocmd cmdId = nil return true end, }) vim.lsp.buf.rename() -- if LPS couldn't trigger rename on the symbol, clear the autocmd vim.defer_fn(function() -- the cmdId is not nil only if the LSP failed to rename if cmdId then vim.api.nvim_del_autocmd(cmdId) end end, 500) end, { desc = "Rename symbol" }) map("n", "wa", function() vim.lsp.buf.add_workspace_folder() end, { desc = "Add workspace folder" }) map("n", "wr", function() vim.lsp.buf.remove_workspace_folder() end, { desc = "Remove workspace folder" }) map("n", "wl", function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end, { desc = "List workspace folders" }) ---- Debugging with dap ---- map("n", "cdt", ":DapToggleBreakpoint") map("n", "cdx", ":DapTerminate") map("n", "cdo", ":DapStepOver") ---- Completion ---- -- map({ "i", "s" }, "", function() -- if require("cmp").visible() then -- require("nvim-cmp").select_next_item() -- elseif require("luasnip").expand_or_jumpable() then -- vim.fn.feedkeys(vim.api.nvim_replace_termcodes("luasnip-expand-or-jump", true, true, true), "") -- end -- end, { desc = "Next completion item" }) ---- Telescope find ---- map("n", "ff", " Telescope find_files ", { desc = "Find files" }) map("n", "fa", " Telescope find_files follow=true no_ignore=true hidden=true ", { desc = "Find all" }) map("n", "fw", " Telescope live_grep ", { desc = "Live grep" }) map("n", "fb", " Telescope buffers ", { desc = "Find buffers" }) map("n", "", " Telescope buffers ", { desc = "Find buffers" }) map("n", "fh", " Telescope help_tags ", { desc = "Help page" }) map("n", "fo", " Telescope oldfiles ", { desc = "Fild old files" }) map("n", "fz", " Telescope current_buffer_fuzzy_find ", { desc = "Find in current buffer" }) ----------------------------------------------------------- -- better up/down -- map({ "n", "x" }, "j", "v:count == 0 ? 'gj' : 'j'", { desc = "Down", expr = true, silent = true }) -- map({ "n", "x" }, "", "v:count == 0 ? 'gj' : 'j'", { desc = "Down", expr = true, silent = true }) -- map({ "n", "x" }, "k", "v:count == 0 ? 'gk' : 'k'", { desc = "Up", expr = true, silent = true }) -- map({ "n", "x" }, "", "v:count == 0 ? 'gk' : 'k'", { desc = "Up", expr = true, silent = true }) ---- Movement ---- -- Move Lines map("n", "", "m .+1==", { desc = "Move Down" }) map("n", "", "m .-2==", { desc = "Move Up" }) map("i", "", "m .+1==gi", { desc = "Move Down" }) map("i", "", "m .-2==gi", { desc = "Move Up" }) map("v", "", ":m '>+1gv=gv", { desc = "Move Down" }) map("v", "", ":m '<-2gv=gv", { desc = "Move Up" }) -- Clear search with map({ "i", "n" }, "", "noh", { desc = "Escape and Clear hlsearch" }) -- Clear search, diff update and redraw -- taken from runtime/lua/_editor.lua map( "n", "ur", "nohlsearchdiffupdatenormal! ", { desc = "Redraw / Clear hlsearch / Diff Update" } ) -- https://github.com/mhinz/vim-galore#saner-behavior-of-n-and-n map("n", "n", "'Nn'[v:searchforward].'zv'", { expr = true, desc = "Next Search Result" }) map("x", "n", "'Nn'[v:searchforward]", { expr = true, desc = "Next Search Result" }) map("o", "n", "'Nn'[v:searchforward]", { expr = true, desc = "Next Search Result" }) map("n", "N", "'nN'[v:searchforward].'zv'", { expr = true, desc = "Prev Search Result" }) map("x", "N", "'nN'[v:searchforward]", { expr = true, desc = "Prev Search Result" }) map("o", "N", "'nN'[v:searchforward]", { expr = true, desc = "Prev Search Result" }) ----------------------------------------------------------- -- save file map({ "i", "x", "n", "s" }, "", "w", { desc = "Save File" }) -- better indenting map("v", "<", "", ">gv") -- new file map("n", "nf", "enew", { desc = "New File" }) map("n", "", "enew", { desc = "New File" }) map("n", "[q", vim.cmd.cprev, { desc = "Previous Quickfix" }) map("n", "]q", vim.cmd.cnext, { desc = "Next Quickfix" }) ---- Code ---- -- formatting map({ "n", "v" }, "cf", function() require("conform").format() end, { desc = "Format" }) -- diagnostic local diagnostic_goto = function(next, severity) local go = next and vim.diagnostic.goto_next or vim.diagnostic.goto_prev severity = severity and vim.diagnostic.severity[severity] or nil return function() go({ severity = severity }) end end map("n", "cd", vim.diagnostic.open_float, { desc = "Line Diagnostics" }) map("n", "]d", diagnostic_goto(true), { desc = "Next Diagnostic" }) map("n", "[d", diagnostic_goto(false), { desc = "Prev Diagnostic" }) map("n", "]e", diagnostic_goto(true, "ERROR"), { desc = "Next Error" }) map("n", "[e", diagnostic_goto(false, "ERROR"), { desc = "Prev Error" }) map("n", "]w", diagnostic_goto(true, "WARN"), { desc = "Next Warning" }) map("n", "[w", diagnostic_goto(false, "WARN"), { desc = "Prev Warning" }) ---- Toggle options ---- map("n", "um", function() require("monkey-alert").enable() -- does not toggle :O end, { desc = "Toggle monkey alert" }) map("n", "uf", function() LazyVim.format.toggle() end, { desc = "Toggle Auto Format (Global)" }) map("n", "uF", function() LazyVim.format.toggle(true) end, { desc = "Toggle Auto Format (Buffer)" }) map("n", "us", function() LazyVim.toggle("spell") end, { desc = "Toggle Spelling" }) map("n", "uw", function() LazyVim.toggle("wrap") end, { desc = "Toggle Word Wrap" }) map("n", "uL", function() LazyVim.toggle("relativenumber") end, { desc = "Toggle Relative Line Numbers" }) map("n", "ul", function() LazyVim.toggle.number() end, { desc = "Toggle Line Numbers" }) map("n", "ud", function() LazyVim.toggle.diagnostics() end, { desc = "Toggle Diagnostics" }) local conceallevel = vim.o.conceallevel > 0 and vim.o.conceallevel or 3 map("n", "uc", function() LazyVim.toggle("conceallevel", false, { 0, conceallevel }) end, { desc = "Toggle Conceal" }) if vim.lsp.buf.inlay_hint or vim.lsp.inlay_hint then map("n", "uh", function() LazyVim.toggle.inlay_hints() end, { desc = "Toggle Inlay Hints" }) end map("n", "uT", function() if vim.b.ts_highlight then vim.treesitter.stop() else vim.treesitter.start() end end, { desc = "Toggle Treesitter Highlight" }) map("n", "ub", function() LazyVim.toggle("background", false, { "light", "dark" }) end, { desc = "Toggle Background" }) -- aerial map("n", "ua", "AerialToggle", { desc = "Toggle aerial view" }) --------------------------------------------------------------------------- -- highlights under cursor map("n", "ui", vim.show_pos, { desc = "Inspect Pos" }) map("n", "uI", "InspectTree", { desc = "Inspect Tree" }) ---- Terminal ---- map("n", "nz", "terminal", { desc = "Open new terminal" }) -- Terminal Mappings map("t", "", "", { desc = "Enter Normal Mode" }) map("t", "", "wincmd h", { desc = "Go to Left Window" }) map("t", "", "wincmd j", { desc = "Go to Lower Window" }) map("t", "", "wincmd k", { desc = "Go to Upper Window" }) map("t", "", "wincmd l", { desc = "Go to Right Window" }) map("t", "", "close", { desc = "Hide Terminal" }) map("t", "", "close", { desc = "which_key_ignore" }) --------------------------------------------------------------------------- ---- Windows ---- map("n", "ww", "p", { desc = "Other Window", remap = true }) map("n", "wd", "c", { desc = "Delete Window", remap = true }) map("n", "w-", "s", { desc = "Split Window Below", remap = true }) map("n", "w|", "v", { desc = "Split Window Right", remap = true }) map("n", "-", "s", { desc = "Split Window Below", remap = true }) map("n", "|", "v", { desc = "Split Window Right", remap = true }) map("n", "wm", function() LazyVim.toggle.maximize() end, { desc = "Maximize Toggle" }) -- Move to window using the hjkl keys map("n", "", "h", { desc = "Go to Left Window", remap = true }) map("n", "", "j", { desc = "Go to Lower Window", remap = true }) map("n", "", "k", { desc = "Go to Upper Window", remap = true }) map("n", "", "l", { desc = "Go to Right Window", remap = true }) map("n", "", "resize +2", { desc = "Increase Window Height" }) map("n", "", "resize -2", { desc = "Decrease Window Height" }) map("n", "", "vertical resize -2", { desc = "Decrease Window Width" }) map("n", "", "vertical resize +2", { desc = "Increase Window Width" }) -- Resize window using arrow keys map("n", "", "resize +2", { desc = "Increase Window Height" }) map("n", "", "resize -2", { desc = "Decrease Window Height" }) map("n", "", "vertical resize -2", { desc = "Decrease Window Width" }) map("n", "", "vertical resize +2", { desc = "Increase Window Width" }) -- quit map("n", "qq", "qa", { desc = "Quit All" }) --------------------------------------------------------------------------- ---- Utils ---- -- undotree map("n", "ut", "UndotreeToggle", { desc = "Toggle undotree" }) -- aerial map("n", "a", "AerialToggle", { desc = "Toggle aerial view" }) map("n", "]a", "AerialNext", { desc = "Next aerial view" }) map("n", "[a", "AerialPrev", { desc = "Prev aerial view" })