The simple way to remap key in lua is:
vim.api.nvim_set_keymap('n', 'foo', 'bar', { noremap = true, silent = true })
But I want a simple syntax like vimScript: nnoremap foo bar
n_keymap('foo','bar')
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityA simple way to do it is like this:
local n_keymap = function(lhs, rhs)
vim.api.nvim_set_keymap('n', lhs, rhs, { noremap = true, silent = true })
end
n_keymap('foo','bar')
But in my opinion that doesn't make any sense because the map
function are quite powerful (they support different modes, recursive or not, silent or not, expression mappings, buffer local mappings, etc...) so
So my recommendation would be: do use that.
I have found a slightly different solution here:
local function map(mode, lhs, rhs, opts)
local options = {noremap = true}
if opts then options = vim.tbl_extend('force', options, opts) end
vim.api.nvim_set_keymap(mode, lhs, rhs, options)
end
Bellow, you have some example mappings using the proposed function
-- <Tab> to navigate the completion menu
map('i', '<S-Tab>', 'pumvisible() ? "\\<C-p>" : "\\<Tab>"', {expr = true})
map('i', '<Tab>', 'pumvisible() ? "\\<C-n>" : "\\<Tab>"', {expr = true})
map('n', '<C-l>', '<cmd>noh<CR>') -- clears highlights
map("i", "<s-cr>", "<c-o>o") -- adds new line below (insert)
map("i", "<c-cr>", "<c-o>O") -- adds new line above (insert)
-- It adds motions like 25j and 30k to the jump list, so you can cycle
-- through them with control-o and control-i.
-- source: https://www.vi-improved.org/vim-tips/
map("n", "j", [[v:count ? (v:count > 5 ? "m'" . v:count : '') . 'j' : 'gj']], { expr = true })
map("n", "k", [[v:count ? (v:count > 5 ? "m'" . v:count : '') . 'k' : 'gk']], { expr = true })
This is a bug. On vim this menu used to be horizontal. They made it vertical on neovim but they didn't change the controls. You can fix it like:
Lua
vim.api.nvim_set_keymap('c', '<Up>', 'wildmenumode() ? "<Left>" : "<Up>"', {expr = true, noremap=true})
vim.api.nvim_set_keymap('c', '<Down>', 'wildmenumode() ? "<Right>" : "<Down>"', {expr = true, noremap=true})
vim.api.nvim_set_keymap('c', '<Left>', 'wildmenumode() ? "<Up>" : "<Left>"', {expr = true, noremap=true})
vim.api.nvim_set_keymap('c', '<Right>', 'wildmenumode() ? "<Down>" : "<Right>"', {expr = true, noremap=true})
Vimscript alternative
cnoremap <expr> <Up> wildmenumode() ? '<Left>' : '<Up>'
cnoremap <expr> <Down> wildmenumode() ? '<Right>' : '<Down>'
cnoremap <expr> <Left> wildmenumode() ? '<Up>' : '<Left>'
cnoremap <expr> <Right> wildmenumode() ? '<Down>' : '<Right>'
vim.api.nvim_set_keymap
this just looks ugly and too verbose, and it not clear to anybody not used to the neovim stuffinit.lua
vs.init.vim
might be fractionally faster (probably not perceptually so unless your vimrc is huge), but running the mappings is not going to be faster: it just modifies the same internal table in Neovim.