1

In my init.vim, I have the following:

ca WQ wq

What would be the equivalent in my init.lua?

2 Answers 2

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 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 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 at 16:24
  • Ah okay, tnx. I'm using latest stable, 0.9.2
    – ruuter
    Oct 7 at 19:50
2

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

vim.cmd('ca WQ wq')
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 at 16:16

Your Answer

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

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