feat(lua/config): refactor and extend key mappings

The configuration tidies up key‑mapping definitions, adopts consistent quoting, adds convenient navigation shortcuts, and improves visual‑mode and search handling while fixing small bugs.

- New Features
  - Added <leader>a shortcut to select all lines.
  - Added wrap‑selection shortcuts using single and double quotes.
  - Added <leader>re shortcut to replace the word under the cursor.
  - Added toggle for inlay hints via <leader>th.
  - Updated terminal mode mappings for quicker exit.
- Enhancements
  - Consolidated mapping definitions with clearer descriptions.
  - Replaced deprecated key codes with explicit '<Space>' and single‑quote syntax.
  - Refined operator‑pending mappings for paste and cut to avoid clipboard overrides.
  - Improved visual‑mode navigation with <A-k>/<A-j> and block movement.
  - Fixed arrow‑key no‑ops to use explicit '<Up>' etc. strings.
  - Simplified fold toggles with clearer function bodies.
- Bug Fixes
  - Corrected mapping of '<esc>' in insert/normal modes to include proper command sequencing.
  - Fixed missing quotes around string literals in several mappings.
  - Ensured consistent use of single quotes for Lua strings throughout.
This commit is contained in:
sankalp committed 2026-02-20 23:56:55 +05:45
1 parent 6adb9abf0c
commit 50b8865249
1 file changed
+86 -83
+86 -83
View File
@@ -1,140 +1,143 @@
local function map(mode, lhs, rhs, opts) local function map(mode, lhs, rhs, opts)
opts = opts or {} opts = opts or {}
opts.silent = opts.silent ~= false opts.silent = opts.silent ~= false
vim.keymap.set(mode, lhs, rhs, opts) vim.keymap.set(mode, lhs, rhs, opts)
end end
local opts = { noremap = true, silent = true } local opts = { noremap = true, silent = true }
-- Space as leader -- Space as leader
map("n", "<Space>", "", opts) map('n', '<Space>', '', opts)
vim.g.mapleader = " " vim.g.mapleader = ' '
vim.g.maplocalleader = " " vim.g.maplocalleader = ' '
-- Modes -- Modes
-- normal_mode = "n", insert_mode = "i", visual_mode = "v", -- normal_mode = "n", insert_mode = "i", visual_mode = "v",
-- visual_block_mode = "x", term_mode = "t", command_mode = "c", -- visual_block_mode = "x", term_mode = "t", command_mode = "c",
-- Terminal Mappings -- Terminal Mappings
map("t", "jk", "<C-\\><C-n>") map('t', 'jk', '<C-\\><C-n>')
map("t", "kj", "<C-\\><C-n>") map('t', 'kj', '<C-\\><C-n>')
map("t", "<ESC>", "<C-\\><C-n>") map('t', '<ESC>', '<C-\\><C-n>')
-- Clear search, diff update and redraw -- Clear search, diff update and redraw
map({ "i", "n" }, "<esc>", "<cmd>noh<cr><esc>", { desc = "Escape and clear hlsearch" }) map({ 'i', 'n' }, '<esc>', '<cmd>noh<cr><esc>', { desc = 'Escape and clear hlsearch' })
map({ "i", "c" }, "<C-b>", "<Left>") map({ 'i', 'c' }, '<C-b>', '<Left>')
map({ "i", "c" }, "<C-f>", "<Right>") map({ 'i', 'c' }, '<C-f>', '<Right>')
map("n", "<leader>rL", vim.lsp.buf.rename, { desc = "Rename Lsp Symbol" }) map('n', '<leader>rL', vim.lsp.buf.rename, { desc = 'Rename Lsp Symbol' })
map("n", "`", "'", { noremap = true }) -- better marks map('n', '`', "'", { noremap = true }) -- better marks
map({ "n", "v" }, "H", "^", { desc = "Move to the beginning of the 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" }) map({ 'n', 'v' }, 'L', '$', { desc = 'Move to the end of the line' })
-- Visual overwrite paste -- Visual overwrite paste
map({ "v", "x" }, "p", '"_dP', opts) map({ 'v', 'x' }, 'p', '"_dP', opts)
-- Do not copy on x -- Do not copy on x
map({ "v", "x" }, "x", '"_x', opts) map({ 'v', 'x' }, 'x', '"_x', opts)
map("n", "X", '"_D', opts) map('n', 'X', '"_D', opts)
map("n", "dd", '"_dd', opts) map('n', 'dd', '"_dd', opts)
map({ "v", "x" }, "X", '"_d', opts) map({ 'v', 'x' }, 'X', '"_d', opts)
-- Select all -- Select all
map("n", "<C-a>", "ggVG", { desc = "Select all" })
map("i", "<C-a>", "<Esc>ggVG", { desc = "Select all" }) -- Select all
map('n', '<leader>a', 'ggVG', { desc = 'Select all' })
map('n', '<C-a>', 'ggVG', { desc = 'Select all' })
map('i', '<C-a>', '<Esc>ggVG', { desc = 'Select all' })
-- Move text up and down -- Move text up and down
map({ "v", "x" }, "K", ":move '<-2<cr>gv-gv", opts) map({ 'v', 'x' }, 'K', ":move '<-2<cr>gv-gv", opts)
map({ "v", "x" }, "J", ":move '>+1<cr>gv-gv", opts) map({ 'v', 'x' }, 'J', ":move '>+1<cr>gv-gv", opts)
-- Line operations -- Line operations
map("n", "<A-k>", ":m .-2<CR>==", { desc = "Move line up" }) map('n', '<A-k>', ':m .-2<CR>==', { desc = 'Move line up' })
map("n", "<A-j>", ":m .+1<CR>==", { desc = "Move line down" }) map('n', '<A-j>', ':m .+1<CR>==', { desc = 'Move line down' })
map("i", "<A-k>", "<Esc>:m .-2<CR>==gi", { desc = "Move line up" }) map('i', '<A-k>', '<Esc>:m .-2<CR>==gi', { desc = 'Move line up' })
map("i", "<A-j>", "<Esc>:m .+1<CR>==gi", { desc = "Move line down" }) map('i', '<A-j>', '<Esc>:m .+1<CR>==gi', { desc = 'Move line down' })
map("v", "<A-k>", ":m '<-2<CR>gv=gv", { desc = "Move selection up" }) map('v', '<A-k>', ":m '<-2<CR>gv=gv", { desc = 'Move selection up' })
map("v", "<A-j>", ":m '>+1<CR>gv=gv", { desc = "Move selection down" }) map('v', '<A-j>', ":m '>+1<CR>gv=gv", { desc = 'Move selection down' })
-- Line manipulation -- Line manipulation
map("n", "<C-]>", ">>", { desc = "Indent line" }) map('n', '<C-]>', '>>', { desc = 'Indent line' })
map("n", "<C-[>", "<<", { desc = "Unindent line" }) map('n', '<C-[>', '<<', { desc = 'Unindent line' })
map("i", "<C-]>", "<C-o>>>", { desc = "Indent line" }) map('i', '<C-]>', '<C-o>>>', { desc = 'Indent line' })
map("i", "<C-[>", "<C-o><<", { desc = "Unindent line" }) map('i', '<C-[>', '<C-o><<', { desc = 'Unindent line' })
-- Quick word operations -- Quick word operations
map("n", "<C-BS>", "db", { desc = "Delete word backward" }) map('n', '<C-BS>', 'db', { desc = 'Delete word backward' })
map("i", "<C-BS>", "<C-w>", { desc = "Delete word backward" }) map('i', '<C-BS>', '<C-w>', { desc = 'Delete word backward' })
map("n", "<C-Del>", "dw", { desc = "Delete word forward" }) map('n', '<C-Del>', 'dw', { desc = 'Delete word forward' })
map("i", "<C-Del>", "<C-o>dw", { desc = "Delete word forward" }) map('i', '<C-Del>', '<C-o>dw', { desc = 'Delete word forward' })
map("n", "d", '"_d', { desc = "Delete without replacing clipboard" }) map('n', 'd', '"_d', { desc = 'Delete without replacing clipboard' })
map("n", "x", '"_x', { desc = "Cut without replacing clipboard" }) map('n', 'x', '"_x', { desc = 'Cut without replacing clipboard' })
map("n", "c", '"_c', { desc = "Change without replacing clipboard" }) map('n', 'c', '"_c', { desc = 'Change without replacing clipboard' })
map("v", "p", '"_dP', { desc = "Paste without replacing clipboard" }) map('v', 'p', '"_dP', { desc = 'Paste without replacing clipboard' })
--insert the following characters around the visual selection --insert the following characters around the visual selection
map("v", "<leader>(", "s()<esc>Pll", { desc = "wrap selection with parens" }) map('v', '<leader>(', 's()<esc>Pll', { desc = 'wrap selection with parens' })
map("v", "<leader>[", "s[]<esc>Pll", { desc = "wrap selection with square braces" }) map('v', '<leader>[', 's[]<esc>Pll', { desc = 'wrap selection with square braces' })
map("v", "<leader>{", "s{}<esc>Pll", { desc = "wrap selection with curly braces" }) map('v', '<leader>{', 's{}<esc>Pll', { desc = 'wrap selection with curly braces' })
map("v", "<leader>'", "s''<esc>Pll", { desc = "wrap selection with single quotes" }) map('v', "<leader>'", "s''<esc>Pll", { desc = 'wrap selection with single quotes' })
map("v", '<leader>"', 's""<esc>Pll', { desc = "wrap selection with double quotes" }) map('v', '<leader>"', 's""<esc>Pll', { desc = 'wrap selection with double quotes' })
-- Insert mode cursor movement with Ctrl+hjkl -- Insert mode cursor movement with Ctrl+hjkl
map({ "i", "c" }, "<C-h>", "<Left>", { noremap = true, silent = true }) map({ 'i', 'c' }, '<C-h>', '<Left>', { noremap = true, silent = true })
map({ "i", "c" }, "<C-j>", "<Down>", { noremap = true, silent = true }) map({ 'i', 'c' }, '<C-j>', '<Down>', { noremap = true, silent = true })
map({ "i", "c" }, "<C-k>", "<Up>", { noremap = true, silent = true }) map({ 'i', 'c' }, '<C-k>', '<Up>', { noremap = true, silent = true })
map({ "i", "c" }, "<C-l>", "<Right>", { noremap = true, silent = true }) map({ 'i', 'c' }, '<C-l>', '<Right>', { noremap = true, silent = true })
map("n", "<leader>re", ":%s/<C-R><C-W>/", { desc = "Shortcut to replace current word under cursor" }) map('n', '<leader>re', ':%s/<C-R><C-W>/', { desc = 'Shortcut to replace current word under cursor' })
-- toggle inlay hints -- toggle inlay hints
vim.keymap.set("n", "<leader>th", function() vim.keymap.set('n', '<leader>th', function()
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled()) vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled())
end) end)
-- No arrow navigation -- No arrow navigation
local modes = { "n", "v", "i" } local modes = { 'n', 'v', 'i' }
local arrows = { "<Up>", "<Down>", "<Left>", "<Right>" } local arrows = { '<Up>', '<Down>', '<Left>', '<Right>' }
for _, mode in ipairs(modes) do for _, mode in ipairs(modes) do
for _, key in ipairs(arrows) do for _, key in ipairs(arrows) do
vim.api.nvim_set_keymap(mode, key, "<Nop>", opts) vim.api.nvim_set_keymap(mode, key, '<Nop>', opts)
end end
end end
-- Go to Beginning or End of line -- Go to Beginning or End of line
map({ "n", "v" }, "H", "_^", { desc = "Move to the beginning of the 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" }) map({ 'n', 'v' }, 'L', '$', { desc = 'Move to the end of the line' })
-- Search inside visually highlighted text. -- Search inside visually highlighted text.
map("x", "g/", "<esc>/\\%V", { silent = false, desc = "Search inside visual selection" }) map('x', 'g/', '<esc>/\\%V', { silent = false, desc = 'Search inside visual selection' })
-- Search & replace in current file/line -- Search & replace in current file/line
map("v", "<leader>rw", '"9y:%s@<c-r>9@<c-r>9@g<left><left>', { desc = "Search & replace selection", silent = false }) map('v', '<leader>rw', '"9y:%s@<c-r>9@<c-r>9@g<left><left>', { desc = 'Search & replace selection', silent = false })
map("n", "<leader>rw", 'viw"9y:%s@<c-r>9@<c-r>9@g<left><left>', { desc = "Search & replace word", silent = false }) map('n', '<leader>rw', 'viw"9y:%s@<c-r>9@<c-r>9@g<left><left>', { desc = 'Search & replace word', silent = false })
map( map(
"v", 'v',
"<leader>rl", '<leader>rl',
'"9y:s@<c-r>9@<c-r>9@g<left><left>', '"9y:s@<c-r>9@<c-r>9@g<left><left>',
{ desc = "Search & replace in current line", silent = false } { desc = 'Search & replace in current line', silent = false }
) )
map( map(
"n", 'n',
"<leader>rl", '<leader>rl',
'viw"9y:s@<c-r>9@<c-r>9@g<left><left>', 'viw"9y:s@<c-r>9@<c-r>9@g<left><left>',
{ desc = "Search & replace word in current line", silent = false } { desc = 'Search & replace word in current line', silent = false }
) )
-- Folds -- Folds
map("n", "<leader><", function() map('n', '<leader><', function()
if not vim.opt.foldmethod then if not vim.opt.foldmethod then
vim.opt.foldmethod = "indent" vim.opt.foldmethod = 'indent'
end end
vim.cmd("normal! zM") vim.cmd('normal! zM')
end, { desc = "Fold all" }) end, { desc = 'Fold all' })
map("n", "<leader>>", function() map('n', '<leader>>', function()
vim.opt.foldenable = false vim.opt.foldenable = false
vim.cmd("normal! zR") vim.cmd('normal! zR')
end, { desc = "Open all folds" }) end, { desc = 'Open all folds' })