2

Say I have a normal mode mapping on key P, for sure it can be executed in command mode:

:lua normal! P

Now I want to do this in lua, so I tried:

vim.cmd('normal! P')

and NeoVim executed it as "paste from clipboard", which caused some error since the buffer I'm working on is not modifiable. If I want it to execute what I have remapped on P, how?

update: I have tried removing the !, but it seems that it doesn't solve the problem :/

3
  • 2
    Why do you need :lua normal[!]? Isn't just :normal[!] enough?
    – D. Ben Knoble
    Sep 27, 2022 at 12:41
  • 2
    I have no idea, why you want to use :lua to execute a normal mode command. Also, please note, if the current buffer is not modifiable, an error will be expected. You should rather script something around it, to check whether the current buffer is modifiable (or readonly) or not and depending on the result perform the paste action Sep 28, 2022 at 7:50
  • If you've already received an answer, please don't edit the question to ask a different question. (your original question didn't mention vim.cmd at all). It's not fair to the person that took the time to write up an answer for you,. Feel free to edit the question if you need to clarify it: otherwise you should create a new question.
    – Rich
    Oct 27, 2022 at 9:52

1 Answer 1

1

:normal runs normal mode commands. If the ! is given, mappings will not be used. Without it when this command is called from a non-remappable mapping(:noremap), the argument can be mapped anyway.

So when mapping, don't use noremap and omit !.

:lua normal p

Now will execute what you have remapped.

2
  • 1
    In lua there is a vim.cmd() that can execute :lua .... And in this case I your answer would mean just removing the !. I did, and it still doesn't work. I have edited my question to be clear. Sep 27, 2022 at 6:29
  • Did you also disable noremap option ? With this option enabled, mappings will not be used even if you remove !. I recorded some macro and mapped it to p . With both ! and noremap option enabled, it was still pasting with p. But when I did this vim.api.nvim_set_keymap('n', 'p', '@q', {noremap=false}), :lua normal p was doing whatever was recorded on q key. Sep 27, 2022 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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