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", -- Use jj as escape map('i', 'jj', '', opts) map('i', 'jk', '', opts) map('i', 'kj', '', opts) map('t', 'JJ', '', opts) -- Terminal Mappings map('t', 'jk', '') map('t', 'kj', '') map('t', '', '') -- Visual overwrite paste map({ 'v', 'x' }, 'p', '"_dP', opts) -- Do not copy on x map({ 'v', 'x' }, 'x', '"_x', opts) -- VSCode style shortcuts -- File operations map('n', '', ':w', { desc = 'Save file' }) map('i', '', ':wa', { desc = 'Save file' }) map('n', '', ':q', { desc = 'Quit' }) map('n', '', ':bd', { desc = 'Close buffer' }) -- Undo/Redo map('n', '', 'u', { desc = 'Undo' }) map('i', '', 'u', { desc = 'Undo' }) map('n', '', '', { desc = 'Redo' }) map('i', '', '', { desc = 'Redo' }) -- Select all map('n', '', 'ggVG', { desc = 'Select all' }) map('i', '', 'ggVG', { desc = 'Select all' }) -- Copy/Cut map('v', '', 'y', { desc = 'Copy' }) map('v', '', 'd', { desc = 'Cut' }) -- Line operations (VSCode style) 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' }) -- VSCode-style line operations map('n', '', 'o', { desc = 'Insert line below' }) map('i', '', 'o', { desc = 'Insert line below' }) -- 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' }) -- Duplicate line map('n', '', 'yykP', { desc = 'Duplicate line up' }) map('n', '', 'yyp', { desc = 'Duplicate line down' }) map('v', '', "y'', "y'>pgv", { desc = 'Duplicate selection down' }) -- Line manipulation map('n', '', '>>', { desc = 'Indent line' }) map('n', '', '<<', { desc = 'Unindent line' }) map('i', '', '>>', { desc = 'Indent line' }) map('i', '', '<<', { desc = 'Unindent 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({ 'v', 'x' }, 'X', '"_d', opts) -- Increment/decrement map({ 'n', 'v', 'x' }, '-', '', opts) map({ 'n', 'v', 'x' }, '=', '', opts) -- Move to line beginning and end map({ 'n', 'v', 'x' }, 'gl', '$', { desc = 'End of line' }) map({ 'n', 'v', 'x' }, 'gh', '^', { desc = 'Beginning of line' }) -- Center Cursors map('n', 'J', 'mzJ`z', opts) map('n', '', 'zz', opts) map('n', '', 'zz', opts) map('n', 'n', 'nzzzv', opts) map('n', 'N', 'Nzzzv', opts) -- Better up/down map('n', 'j', "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true }) map('n', 'k', "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = true }) -- Move text up and down map({ 'v', 'x' }, 'J', ":move '>+1gv-gv", opts) map({ 'v', 'x' }, 'K', ":move '<-2gv-gv", opts) -- Clear search, diff update and redraw map({ 'i', 'n' }, '', 'noh', { desc = 'Escape and clear hlsearch' }) -- Consistent n/N search navigation map('n', 'n', "'Nn'[v:searchforward]", { 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]", { 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' }) -- Better indenting map('v', '<', '', '>gv') -- Add undo breakpoints map('i', ',', ',u') map('i', '.', '.u') map('i', ';', ';u') 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' }) -- Toggle relative numbers off or on (for code presentation) map({ 'n', 'v' }, 'tn', 'set rnu!', {}) --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', '', '', { desc = 'Move cursor left' }) map('i', '', '', { desc = 'Move cursor down' }) map('i', '', '', { desc = 'Move cursor up' }) map('i', '', '', { desc = 'Move cursor right' })