1

In my init.vim, I have the following:

ca WQ wq

What would be the equivalent in my init.lua?

3 Answers 3

3

!!Note: this solution only works on Neovim >= v0.10; see @ruuter's answer if you are on an older version of neovim.

Command abbreviation is treated as just another keymap mode by nvim_set_keymap. The equivalent of ca WQ wq in lua is:

vim.keymap.set("ca", "WQ", "wq")

I had a hard time discovering this, as the help text in h: vim.keymap.set/the lua.txt help file does not actually list out the modes! It does however tell you to see h: nvim_set_keymap which lives in the api.txt help file, and that's where abbreviations are mentioned!

4
  • Good answer (voted), but I can’t resist pointing out that this is yet-another-easier-in-plain-Vim(script) moment :)
    – D. Ben Knoble
    Jul 4, 2023 at 15:55
  • This does not seem to work for me. I get Shortname is too long error. Instead this you can simply do vim.cmd('ca WQ wq'). Use vim.cmd to execute any vim command in lua script.
    – ruuter
    Oct 3, 2023 at 13:11
  • 1
    @ruuter what version of neovim are you using? It looks like support for abbreviated short names for this API was only added 4 months ago to nightly (commit). Oct 7, 2023 at 16:24
  • Ah okay, tnx. I'm using latest stable, 0.9.2
    – ruuter
    Oct 7, 2023 at 19:50
2

You can execute any vim script command with vim.cmd. So in your case:

vim.cmd('ca WQ wq')
1
  • 1
    While this was the solution I was using prior to making this question, my intention was to understand how to do this purely via Lua (i.e. no escape hatch to vimscript). Oct 7, 2023 at 16:16
1
vim.keymap.set('c',  'WQ', 'wq') 

works for me on

NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1703358377

where 'c' is for command mode. See Neovim docs for details.

For instance, I've just used it to make help output into a vertical split:

vim.keymap.set('c', 'h', 'vert h') 

Update

It sort of works, but it makes the above substitution whenever you type h. I changed back to vim.cmd solution myself.

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.