local M = {} local Log = require "core.log" local generic_opts_any = { noremap = true, silent = true } local generic_opts = { insert_mode = generic_opts_any, normal_mode = generic_opts_any, visual_mode = generic_opts_any, visual_block_mode = generic_opts_any, command_mode = generic_opts_any, term_mode = { silent = true }, } local mode_adapters = { insert_mode = "i", normal_mode = "n", term_mode = "t", visual_mode = "v", visual_block_mode = "x", command_mode = "c", } -- Append key mappings to lunarvim's defaults for a given mode -- @param keymaps The table of key mappings containing a list per mode (normal_mode, insert_mode, ..) function M.append_to_defaults(keymaps) for mode, mappings in pairs(keymaps) do for k, v in ipairs(mappings) do options.keys[mode][k] = v end end end -- Set key mappings individually -- @param mode The keymap mode, can be one of the keys of mode_adapters -- @param key The key of keymap -- @param val Can be form as a mapping or tuple of mapping and user defined opt function M.set_keymaps(mode, key, val) local opt = generic_opts[mode] and generic_opts[mode] or generic_opts_any if type(val) == "table" then opt = val[2] val = val[1] end vim.api.nvim_set_keymap(mode, key, val, opt) end -- Load key mappings for a given mode -- @param mode The keymap mode, can be one of the keys of mode_adapters -- @param keymaps The list of key mappings function M.load_mode(mode, keymaps) mode = mode_adapters[mode] and mode_adapters[mode] or mode for k, v in pairs(keymaps) do M.set_keymaps(mode, k, v) end end -- Load key mappings for all provided modes -- @param keymaps A list of key mappings for each mode function M.load(keymaps) keymaps = keymaps or {} for mode, mapping in pairs(keymaps) do M.load_mode(mode, mapping) end end function M.get_defaults() local keys = { ---@usage change or add keymappings for insert mode insert_mode = { -- 'jk' for quitting insert mode ["jk"] = "", -- 'kj' for quitting insert mode ["kj"] = "", -- 'jj' for quitting insert mode ["jj"] = "", -- Move current line / block with Alt-j/k ala vscode. [""] = ":m .+1==gi", -- Move current line / block with Alt-j/k ala vscode. [""] = ":m .-2==gi", -- navigation [""] = "k", [""] = "j", [""] = "h", [""] = "l", }, ---@usage change or add keymappings for normal mode normal_mode = { -- Better window movement [""] = "h", [""] = "j", [""] = "k", [""] = "l", -- Resize with arrows [""] = ":resize -2", [""] = ":resize +2", [""] = ":vertical resize -2", [""] = ":vertical resize +2", -- Tab switch buffer [""] = ":BufferNext", [""] = ":BufferPrevious", -- Move current line / block with Alt-j/k a la vscode. [""] = ":m .+1==", [""] = ":m .-2==", -- QuickFix ["]q"] = ":cnext", ["[q"] = ":cprev", [""] = ":call QuickFixToggle()", }, ---@usage change or add keymappings for terminal mode term_mode = { -- Terminal window navigation [""] = "h", [""] = "j", [""] = "k", [""] = "l", }, ---@usage change or add keymappings for visual mode visual_mode = { -- Better indenting ["<"] = ""] = ">gv", -- ["p"] = '"0p', -- ["P"] = '"0P', }, ---@usage change or add keymappings for visual block mode visual_block_mode = { -- Move selected line / block of text in visual mode ["K"] = ":move '<-2gv-gv", ["J"] = ":move '>+1gv-gv", -- Move current line / block with Alt-j/k ala vscode. [""] = ":m '>+1gv-gv", [""] = ":m '<-2gv-gv", }, ---@usage change or add keymappings for command mode command_mode = { -- navigate tab completion with and -- runs conditionally [""] = { 'pumvisible() ? "\\" : "\\"', { expr = true, noremap = true }, }, [""] = { 'pumvisible() ? "\\" : "\\"', { expr = true, noremap = true }, }, }, } if vim.fn.has "mac" == 1 then keys.normal_mode[""] = keys.normal_mode[""] keys.normal_mode[""] = keys.normal_mode[""] keys.normal_mode[""] = keys.normal_mode[""] keys.normal_mode[""] = keys.normal_mode[""] Log:debug "Activated mac keymappings" end return keys end function M.print(mode) print "List of LunarVim's default keymappings (not including which-key)" if mode then print(vim.inspect(options.keys[mode])) else print(vim.inspect(options.keys)) end end return M