0
vim.api.nvim_create_user_command("TestCommand", function(opts)
    print(opts.count)
end, { count = 1 })

vim.keymap.set("n", "<C-x>", "<cmd> TestCommand <CR>")

This will always print 1, even if I call the keybind with a number prefix. Is there a way to pass the count to the user command using the Lua API?

1 Answer 1

2

Since you're using count = 1, i.e., your command can take a count in the "line number position," consider using:

vim.keymap.set("n", "<C-x>", ":TestCommand <CR>")

The default behavior of : is to place the count before the command.

Another option is to create an expression map which uses v:count inside the command

vim.keymap.set("n", "<C-x>", function() 
     return "<cmd>echo " .. vim.v.count .. "<cr>" end, {expr = true})

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.