local function map(mode, lhs, rhs, opts) opts = opts or {} opts.silent = opts.silent ~= false vim.keymap.set(mode, lhs, rhs, opts) end local opts = { noremap = true, silent = true } -- Space as leader map("n", "", "", opts) vim.g.mapleader = " " vim.g.maplocalleader = " " -- Modes -- normal_mode = "n", insert_mode = "i", visual_mode = "v", -- visual_block_mode = "x", term_mode = "t", command_mode = "c", -- Terminal Mappings map("t", "jk", "") map("t", "kj", "") map("t", "", "") -- Clear search, diff update and redraw map({ "i", "n" }, "", "noh", { desc = "Escape and clear hlsearch" }) map({ "i", "c" }, "", "") map({ "i", "c" }, "", "") map("n", "rL", vim.lsp.buf.rename, { desc = "Rename Lsp Symbol" }) map("n", "`", "'", { noremap = true }) -- better marks map({ "n", "v" }, "H", "^", { desc = "Move to the beginning of the line" }) map({ "n", "v" }, "L", "$", { desc = "Move to the end of the line" }) -- Visual overwrite paste map({ "v", "x" }, "p", '"_dP', opts) -- Do not copy on x map({ "v", "x" }, "x", '"_x', opts) map("n", "X", '"_D', opts) map("n", "dd", '"_dd', opts) map({ "v", "x" }, "X", '"_d', opts) -- Select all -- Select all map("n", "a", "ggVG", { desc = "Select all" }) map("n", "", "ggVG", { desc = "Select all" }) map("i", "", "ggVG", { desc = "Select all" }) -- Move text up and down map({ "v", "x" }, "K", ":move '<-2gv-gv", opts) map({ "v", "x" }, "J", ":move '>+1gv-gv", opts) -- Line operations map("n", "", ":m .-2==", { desc = "Move line up" }) map("n", "", ":m .+1==", { desc = "Move line down" }) map("i", "", ":m .-2==gi", { desc = "Move line up" }) map("i", "", ":m .+1==gi", { desc = "Move line down" }) map("v", "", ":m '<-2gv=gv", { desc = "Move selection up" }) map("v", "", ":m '>+1gv=gv", { desc = "Move selection down" }) -- Line manipulation map("n", "", ">>", { desc = "Indent line" }) map("n", "", "<<", { desc = "Unindent line" }) map("i", "", ">>", { desc = "Indent line" }) map("i", "", "<<", { desc = "Unindent line" }) -- Quick word operations map("n", "", "db", { desc = "Delete word backward" }) map("i", "", "", { desc = "Delete word backward" }) map("n", "", "dw", { desc = "Delete word forward" }) map("i", "", "dw", { desc = "Delete word forward" }) map("n", "d", '"_d', { desc = "Delete without replacing clipboard" }) map("n", "x", '"_x', { desc = "Cut without replacing clipboard" }) map("n", "c", '"_c', { desc = "Change without replacing clipboard" }) map("v", "p", '"_dP', { desc = "Paste without replacing clipboard" }) --insert the following characters around the visual selection map("v", "(", "s()Pll", { desc = "wrap selection with parens" }) map("v", "[", "s[]Pll", { desc = "wrap selection with square braces" }) map("v", "{", "s{}Pll", { desc = "wrap selection with curly braces" }) map("v", "'", "s''Pll", { desc = "wrap selection with single quotes" }) map("v", '"', 's""Pll', { desc = "wrap selection with double quotes" }) -- Insert mode cursor movement with Ctrl+hjkl map({ "i", "c" }, "", "", { noremap = true, silent = true }) map({ "i", "c" }, "", "", { noremap = true, silent = true }) map({ "i", "c" }, "", "", { noremap = true, silent = true }) map({ "i", "c" }, "", "", { noremap = true, silent = true }) map("n", "re", ":%s//", { desc = "Shortcut to replace current word under cursor" }) -- toggle inlay hints vim.keymap.set("n", "th", function() vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled()) end) -- No arrow navigation local modes = { "n", "v", "i" } local arrows = { "", "", "", "" } for _, mode in ipairs(modes) do for _, key in ipairs(arrows) do vim.api.nvim_set_keymap(mode, key, "", opts) end end -- Go to Beginning or End of line map({ "n", "v" }, "H", "_^", { desc = "Move to the beginning of the line" }) map({ "n", "v" }, "L", "$", { desc = "Move to the end of the line" }) -- Search inside visually highlighted text. map("x", "g/", "/\\%V", { silent = false, desc = "Search inside visual selection" }) -- Search & replace in current file/line map("v", "rw", '"9y:%s@9@9@g', { desc = "Search & replace selection", silent = false }) map("n", "rw", 'viw"9y:%s@9@9@g', { desc = "Search & replace word", silent = false }) map( "v", "rl", '"9y:s@9@9@g', { desc = "Search & replace in current line", silent = false } ) map( "n", "rl", 'viw"9y:s@9@9@g', { desc = "Search & replace word in current line", silent = false } ) -- Folds map("n", "<", function() if not vim.opt.foldmethod then vim.opt.foldmethod = "indent" end vim.cmd("normal! zM") end, { desc = "Fold all" }) map("n", ">", function() vim.opt.foldenable = false vim.cmd("normal! zR") end, { desc = "Open all folds" })