7

I am writing a lua function as custom command for my neovim config.

As the documentation says "The function is called with a single table argument that contains the following keys", but how can i access these keys when the table is not defined to a variable.

I tried calling the function like this:

vim.api.nvim_create_user_command('Build', fn_build(args), { nargs='?' })

and access the values with:

function fn_build(args)
  run = args["args"] or nil
end

but i would get a nil error.


@Ani commented:

Try to see if there is anything close to this, in github.com/nanotee/nvim-lua-guide

I found the guide, but it didn't helped me to fix it. I'm not sure if desc is the right variable to use. And how would i even use it. The guide says:

Two additional attributes are available:

  1. desc allows you to control what gets displayed when you run :command {cmd} on a command defined as a Lua callback. Similarly to keymaps, it is recommended to add a desc key to commands defined as Lua functions.
  2. force is equivalent to calling :command! and replaces a command if one with the same name already exists. It is true by default, unlike its Vimscript equivalent.

Am i blind and overseeing something?

Please point me in the right direction

1

3 Answers 3

4

You are calling the function fn_build and registering the return value. Instead you should just pass the function as a parameter. Try the following

vim.api.nvim_create_user_command('Build', fn_build, { nargs='?' })
Sign up to request clarification or add additional context in comments.

Comments

3

As supermeia said your function should receive opts, you should be able to do it directly when you create your command if you don't want to change your fn_build function, like this:

 vim.api.nvim_create_user_command(
  function(opts)
    fn_build(opts.args)
  end,
  { nargs = '?' }
)

Comments

2

fn_build should receive opts as an argument try the following:

function fn_build(opts)
    run = opts.args or nil
end

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.