1

Summary

I use a command and it prints a message. When I create a user command that runs the same underlying function, I don't see the message.

Example

The command :Telescope git_status from the Telescope plugin shows information about git status. When there are no git changes, it will print the following message:

No changes found
[telescope.builtin.git_status]: No changes found
Press ENTER or type command to continue

I like this message and I want to see it.

Here is my custom user command:

local telescopeBuiltins = require('telescope.builtin')
vim.api.nvim_create_user_command('Gs',
    function()
        vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<ESC>', true, false, true), 'n', false)
        return telescopeBuiltins.git_status()
    end,
    {bang = true}
)

However, when I run :Gs, I just see nothing on the cmdline. For some reason, the user command is executing "silently" and not showing the error message.

2
  • 1
    Your function doesn’t run :Telescope git_status though; what happens if you do that instead?
    – D. Ben Knoble
    Nov 18 at 15:42
  • @D.BenKnoble if I change it to vim.cmd(':Telescope git_status') instead of telescopeBuiltins.git_status(), I get the same result. I should mention that the functionality works fine when there are actual git changes, i.e. I'm successfully calling the correct underlying function.
    – nullromo
    Nov 20 at 17:56

1 Answer 1

3

I'm assuming you assigned telescopeBuiltins to require("telescope.builtin"), in which case you should be able to do this:

vim.api.nvim_create_user_command(
    "Gs",
    telescopeBuiltins.git_status,
    { bang = true }
)

If you want to pass arguments to your command, that then get passed to the git_status function, you can wrap it in an anonymous function:

vim.api.nvim_create_user_command(
    "Gs",
    function(tbl) telescopeBuiltins.git_status(tbl.args) end,
    { bang = true, nargs = "?" }
)

Edit: I just wanted to clarify that, when defining a user command, a table of information is passed to the provided function by default (see :help nvim_create_user_command). If you don't want this table passed along to the API call, the call should be wrapped in an anonymous function.

2
  • Yes, telescopeBuiltins is equal to require('telescope.builtin'). I edited the question to amend that. Your suggestion worked; I changed function() telescopeBuiltins.git_status() end to telescopeBuiltins.git_status and it worked how I wanted it to. Any ideas why enclosing it in a function changed the behavior?
    – nullromo
    Nov 20 at 17:59
  • Good question! Maybe it has something to do with the return? I'm not sure, to be honest.
    – heygarrett
    Nov 21 at 19:00

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.