Dup Ver Goto 📝

Defining Keybindings in Lua

pt2/editor/neovim 07-31 13:46:47
To
26 lines, 118 words, 1009 chars Monday 2023-07-31 13:46:47
vim.keymap.set('i', '<Tab>', function()
    print("hello world")
end, {expr = true})

or

vim.keymap.set('n', '\hello', "ihello<esc>")

Basically the third argument can be either a lua function, or a string. If a string, it is executed as if vimscript. See neovim docs and in particular nvim_set_keymap() for more. In short:

-- Map to a Lua function:
vim.keymap.set('n', 'lhs', function() print("real lua function") end)
-- Map to multiple modes:
vim.keymap.set({'n', 'v'}, '<leader>lr', vim.lsp.buf.references, { buffer=true })
-- Buffer-local mapping:
vim.keymap.set('n', '<leader>w', "<cmd>w<cr>", { silent = true, buffer = 5 })
-- Expr mapping:
vim.keymap.set('i', '<Tab>', function()
  return vim.fn.pumvisible() == 1 and "<C-n>" or "<Tab>"
end, { expr = true })
-- <Plug> mapping:
vim.keymap.set('n', '[%', '<Plug>(MatchitNormalMultiBackward)')