-1

the simple why 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')

How i can i write this function n_keymap ?

5
  • 2
    if you need the simple syntax, use the VimScript language. – Christian Brabandt Jul 20 at 16:13
  • lua table if powerful ,i cant do anything and the execution is very fast than vimscript – Ayoub El Mhamdi Jul 21 at 17:57
  • Well fine, but it is a configuration file, speed does not really matter here. Also, when reading vim.api.nvim_set_keymap this just looks ugly and too verbose, and it not clear to anybody not used to the neovim stuff – Christian Brabandt Jul 21 at 18:27
  • Note you're not really going to get any performance benefits from using Lua for your mappings vs. VimScript; the parsing of init.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. – Martin Tournoij Jul 22 at 16:18
  • some time, lua not speed than vimscripte , but i migrate all plugin to lua and yours config files , i want to stop use (vim.cmd) for mapping , and keep config file readable if i can, @stalox give me a nice function that simple and i see useful in my case – Ayoub El Mhamdi Jul 24 at 0:54
1

A 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

  1. At one point you will have to incorporate these features into you own function which is basically reinventing the wheel
  2. You won't make the syntax easier or shorter than what already exists in vimscript while keeping it readable
  3. You are creating something super custom that you are the only one to use, that you will need to maintain and which will most likely cost you more time in maintenance than if you'd use the built-in mechanisms.

So my recommendation would be: do use that.

1
  • i have a lot of plugin, for each plugin i have some keymapping, in sepate files, i hate call vimscripte for do this mapping , also i don't like default mapping , so this fun tion that you write , it's help me for use lua anywhere and readable , also i can creat multi function for silent or recursuve, thank you very much – Ayoub El Mhamdi Jul 24 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.