1

I want to optionally accept a range in a user-defined function:

vim.api.nvim_create_user_command('Pizzafy',
  function(args)
     if args.range > 0 then
        -- visual mode, I think
        print(args.line1 .. "-" .. args.line2 .. " pizzas")
     else
        -- normal or insert mode
        print("normal amount of pizzas")
     end
  end,
  { nargs = 0 }
)

But when I call this with a visual selection:

:'<,'>Pizzafy

I get an error: E481: No range allowed. How do I accept (and then parse out) the visual range in a Lua user command?

1 Answer 1

1

I would do:

vim.api.nvim_create_user_command('Pizzafy',
  function(args)
     if args.range > 0 then
        -- visual mode, I think
        print(args.line1 .. "-" .. args.line2 .. " pizzas")
     else
        -- normal or insert mode
        print("normal amount of pizzas")
     end
  end,
  { nargs = 0, range = true }
)

You need to add the range option to allow the command to be used for ranges.

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.