Execute key RHS expression in Vim imperatively

61 Views Asked by At

Given a string of inputs, it is possible to execute it using :normal!: :normal! ihello will go into insert mode and write the text 'hello'.

When mapping keys to expressions we can use a similar, albeit incompatible syntax. In Lua, we could, for example, define

vim.keymap.set('n', 's', '<c-a>y')

Given such a string, '<c-a>y', how would we execute it imperatively from within Lua/Vimscript without a key binding, similar to what is possible with :normal!?

1

There are 1 best solutions below

0
Matt On

"cmd" notation is not a replacement but alternative. Usually, the RHS of a mapping is processed as if (well, almost) typed. However, any "cmd"-thing is being processed much like as a script.

Therefore, you are supposed to strip "cmd" and "cr" off and pass it onto execute, not normal. Of course, after make sure that there is no other <key> notation left.