-- ╭─────────────────────────────────────────────────────────╮ -- │ Function shortcuts for keymap set │ -- ╰─────────────────────────────────────────────────────────╯ -- vim: set ft=lua ts=2 sw=2 tw=0 et cc=80 : -- Keymap set shortcut --@type vim.keymap.set local s = vim.keymap.set -- Handle description ---@param desc string|table? Optional description. Can be a string or a table. ---@return table -- The description as a table. local function handleDesc(desc) if type(desc) == "string" then -- Convert string to table with `desc` as a key -- If the string is empty, just return as an empty description return { desc = desc } elseif type(desc) == "table" then -- If desc doesn't have 'desc' key, combine it with -- others with empty description if not desc.desc then desc.desc = '' return desc end -- Use the table as is return desc else -- Default to an empty table if `desc` is nil or an unsupported type return { desc = '' } end end -- Normal mode keymaps ---@param key string rhs, required ---@param cmd string|function lhs, required ---@param opts table? Options, optional local n = function(key, cmd, opts) s('n', key, cmd, opts) end -- Leader keymap shortcut function -- It prepends '' to the key ---@param key string rhs, required, but will be prepended with '' ---@param cmd string|function lhs, required ---@param opts table|string? Options (or just description), optional local nl = function(key, cmd, opts) opts = handleDesc(opts) n('' .. key, cmd, opts) end -- Local leader keymap shortcut function -- It prepends '' to the key and uses desc from opts ---@param key string rhs, required, but will be prepended with '' ---@param cmd string|function lhs, required ---@param opts table|string description, required local nld = function(key, cmd, opts) opts = handleDesc(opts) nl(key, cmd, opts) end -- Keymap shortcut function with mode defined, good for sorting by rhs ---@param key string rhs, required ---@param mode string|string[] one of n, v, x, or table of modes { 'n', 'v' } ---@param cmd string|function lhs, required ---@param opts string|table description, required local d = function(key, mode, cmd, opts) opts = handleDesc(opts) s(mode, key, cmd, opts) end -- Leader based keymap shortcut function with mode defined ---@param key string rhs, required, but will be prepended with '' ---@param mode string|string[] one of n, v, x, or table of modes { 'n', 'v' } ---@param cmd string|function lhs, required ---@param opts string|table description (or opts), required local ld = function(key, mode, cmd, opts) opts = handleDesc(opts) s(mode, '' .. key, cmd, opts) end -- ╭─────────────────────────────────────────────────────────╮ -- │ Keymaps │ -- ╰─────────────────────────────────────────────────────────╯ -- Disable arrow keys in normal mode n('', ':echo "Use h to move!!"') n('', ':echo "Use l to move!!"') n('', ':echo "Use k to move!!"') n('', ':echo "Use j to move!!"') n('', ':w!', { desc = 'Save', noremap = true }) n('', ':nohlsearch', { desc = 'Clear Search Highlighting' }) -- Buffer operations -- Mappings for buffer management operations like switching, deleting, etc. -- Convention: All mappings start with 'b' followed by the operation nld('bd', ':lua MiniBufremove.delete()', 'Delete') nld('bh', ':bprev', 'Prev') nld('bj', ':bfirst', 'First') nld('bk', ':blast', 'Last') nld('bl', ':bnext', 'Next') nld('bw', ':lua MiniBufremove.wipeout()', 'Wipeout') -- Code nld('cg', ':lua require("neogen").generate()', 'Generate annotations') -- LSP n('', ':lua vim.lsp.buf.signature_help()', { desc = 'Signature' }) n('K', ':Lspsaga hover_doc', { desc = 'Hover Documentation' }) ld('ca', 'n', ':Lspsaga code_action', 'Code Action') ld('cci', 'n', ':Lspsaga incoming_calls', 'Incoming Calls') ld('cco', 'n', ':Lspsaga outgoing_calls', 'Outgoing Calls') ld('cd', 'n', ':Lspsaga show_line_diagnostics', 'Line Diagnostics') ld('cf', { 'n', 'x' }, ':lua vim.lsp.buf.format()', 'Format') ld('ci', 'n', ':Lspsaga implement', 'Implementations') ld('cl', 'n', ':Lspsaga show_cursor_diagnostics', 'Cursor Diagnostics') ld('cp', 'n', ':Lspsaga peek_definition', 'Peek Definition') ld('cr', 'n', ':Lspsaga rename', 'Rename') ld('cR', 'n', ':Lspsaga rename ++project', 'Rename Project wide') ld('cs', 'n', ':Telescope lsp_document_symbols', 'LSP Document Symbols') ld('ct', 'n', ':Lspsaga peek_type_definition', 'Peek Type Definition') ld('cT', 'n', ':Telescope lsp_type_definitions', 'LSP Type Definitions') ld('cu', 'n', ':Lspsaga preview_definition', 'Preview Definition') ld('cv', 'n', ':Lspsaga diagnostic_jump_prev', 'Diagnostic Jump Prev') ld('cw', 'n', ':Lspsaga diagnostic_jump_next', 'Diagnostic Jump Next') -- CommentBox operations -- Mappings for creating and managing comment boxes -- Convention: All mappings start with 'cb' followed by the box type nld('cbb', 'CBccbox', 'CB: Box Title') nld('cbd', 'CBd', 'CB: Remove a box') nld('cbl', 'CBline', 'CB: Simple Line') nld('cbm', 'CBllbox14', 'CB: Marked') nld('cbt', 'CBllline', 'CB: Titled Line') -- Telescope nld('f', ':Telescope find_files', 'Find Files') nld(',', ':Telescope buffers', 'Find existing buffers') nld('/', function() require('telescope.builtin').current_buffer_fuzzy_find( require('telescope.themes').get_dropdown { winblend = 20, previewer = true, } ) end, 'Fuzzily search in current buffer') nld('sc', ':Telescope commands', 'Commands') nld('sd', ':Telescope diagnostics', 'Search Diagnostics') nld('sg', ':Telescope live_grep', 'Search by Grep') nld('sh', ':Telescope highlights', 'List Highlights') nld('sk', ':Telescope keymaps', 'Search Keymaps') nld('sl', ':Telescope luasnip', 'Search LuaSnip') nld('so', ':Telescope oldfiles', 'Old Files') nld('sp', ':lua require("telescope").extensions.lazy_plugins.lazy_plugins()', 'Lazy Plugins') nld('sq', ':Telescope quickfix', 'Quickfix') nld('ss', ':Telescope treesitter', 'Treesitter') nld('st', ':TodoTelescope', 'Search Todos') nld('sw', ':Telescope grep_string', 'Grep String') nld('sx', ':Telescope import', 'Telescope: Import') -- Trouble nld('xd', ':Trouble document_diagnostics', 'Trouble: Document Diagnostics') nld('xl', ':Trouble loclist', 'Trouble: Location List') nld('xq', ':Trouble quickfix', 'Trouble: Quickfix') nld('xw', ':Trouble workspace_diagnostics', 'Trouble: Workspace Diagnostics') nld('xx', ':Trouble diagnostics', 'Trouble: Diagnostic') -- Text manipulation d('<', { 'n', 'v' }, '', { 'n', 'v' }, '>gv', 'Indent Right') d('', { 'n', 'v' }, ":m '<-2gv=gv", 'Move Block Up') d('', { 'n', 'v' }, ":m '>+1gv=gv", 'Move Block Down') -- Other functionality nld('o', function() require('snacks').gitbrowse() end, 'Open repo in browser') -- Toggle settings local function toggle_background() vim.o.bg = vim.o.bg == "light" and "dark" or "light" end nld('tc', ':CloakToggle', 'Cloak: Toggle') nld('te', ':Neotree toggle', 'Toggle Neotree') nld('tl', toggle_background, 'Toggle Light/Dark Mode') nld('tn', ':Noice dismiss', 'Noice: Dismiss Notification') -- Splits n(',', ':vertical resize -10', { desc = 'V Resize -' }) n('.', ':vertical resize +10', { desc = 'V Resize +' }) n('-', ':resize -5', { desc = 'H Resize -' }) n('+', ':resize +5', { desc = 'H Resize +' }) n('=', '=', { desc = 'Equal Size Splits' }) -- Deal with word wrap n('k', "v:count == 0 ? 'gk' : 'k'", { desc = 'Move up', noremap = true, expr = true }) n('j', "v:count == 0 ? 'gj' : 'j'", { desc = 'Move down', noremap = true, expr = true }) -- Quit nld('qf', ':q', 'Quicker close split') nld('qq', function() if vim.fn.confirm("Force save and quit?", "&Yes\n&No", 2) == 1 then vim.cmd('wq!') end end, 'Quit with force saving') nld('qw', ':wq', 'Write and quit') nld('qQ', function() if vim.fn.confirm("Force quit without saving?", "&Yes\n&No", 2) == 1 then vim.cmd('q!') end end, 'Force quit without saving')