vim.keymap
Methods2
function M.set(modes: string | string[], lhs: string | string[], rhs: function | string, opts: vim.keymap.set.Opts | nil) -> nil
Defines a |mapping| of |keycodes| to a function or keycodes. If lhs is a list, defines a mapping for each (mode, lhs) pair.
Examples:
-- Map "x" to a Lua function:
vim.keymap.set('n', 'x', function() print('real lua function') end)
-- Map "<leader>x" to multiple modes for the current buffer:
vim.keymap.set({'n', 'v'}, '<leader>x', vim.lsp.buf.references, { buf = 0 })
-- Map <Tab> to an expression (|:map-<expr>|):
vim.keymap.set('i', '<Tab>', function()
return vim.fn.pumvisible() == 1 and '<C-n>' or '<Tab>'
end, { expr = true })
-- Map "[%%" to a <Plug> mapping:
vim.keymap.set('n', '[%%', '<Plug>(MatchitNormalMultiBackward)')
-- Use `getregionpos(getpos('v'))` to get the "current visual selection":
vim.keymap.set('x', 'M', function()
local region = vim.fn.getregionpos(vim.fn.getpos('v'), vim.fn.getpos('.'), {
type = 'v',
exclusive = false,
eol = false,
})
local line1 = region[1][1][2]
local line2 = region[#region][1][2]
vim.print({ line1, line2 })
end)
vim.keymap.set({ 'n', 'i' }, { 'a', 'b' }, '<cmd>echom localtime()<cr>')
-- ... is the same as:
vim.keymap.set('n', 'a', '<cmd>echom localtime()<cr>')
vim.keymap.set('i', 'a', '<cmd>echom localtime()<cr>')
vim.keymap.set('n', 'b', '<cmd>echom localtime()<cr>')
vim.keymap.set('i', 'b', '<cmd>echom localtime()<cr>')
Parameters
modesstring | string[]Mode "short-name" (see |nvimsetkeymap()|), or a list thereof.
lhsstring | string[]Left-hand side |{lhs}| of the mapping, or a list thereof.
rhsfunction | stringRight-hand side |{rhs}| of the mapping, can be a Lua function.
optsvim.keymap.set.Opts | nil
Returns
- nil
See:
|nvimsetkeymap()| |maparg()| |mapcheck()| |mapset()|
function M.del(modes: string | string[], lhs: string | string[], opts: vim.keymap.del.Opts | nil) -> nil
Removes a mapping, or removes each (mode, lhs) pair if lhs is a list.
Examples:
vim.keymap.del('n', 'lhs')
vim.keymap.del({'n', 'i', 'v'}, '<leader>w', { buf = 5 })
See:
|vim.keymap.set()|