So I was trying to setup a custom command to change tab width on the fly.

function! SetTab(width)
    set tabstop=a:width
:command Tab -nargs=1 call SetTab(<args>)

The idea is to when I type :Tab 2, the current tab width will be changed to 2.

But it doesn't work and gives E488 Trailing characters when I issue the command, not sure where it's coming from, I couldn't find any trailing char.


1 Answer 1


First thing you need to do is get the syntax of :command right. This is what it's supposed to be...

:com[mand][!] [{attr}...] {cmd} {rep}

{attr} means attributes. That includes the -nargs value. Following that is {cmd} which is the command name.

That gives us this...

:command -nargs=1 Tab call SetTab(<args>)

Problem is, it still won't work.

What you need next is to clean up the data you're passing to your function. <args> is not intended to be used as argument(s) to a user function. Instead you should use <f-args>

But don't take my word for it. Quoting :h <f-args>, the purpose is...

...to allow commands to pass their arguments on to a user-defined function, there is a special form ("function args"). This splits the command arguments at spaces and tabs, quotes each argument individually, and the sequence is replaced by the comma-separated list of quoted arguments.

That's just an excerpt. Be sure to read the whole thing! There's also example code following it that does something similar to all this.

Anyways, this is what we have now...

:command -nargs=1 Tab call SetTab(<f-args>)

...and that should do the trick.

  • 1
    It's working now, great!!
    – ramgorur
    Aug 5, 2021 at 7:06
  • 1
    Doesn’t the function also need execute?
    – D. Ben Knoble
    Aug 5, 2021 at 11:49
  • 1
    I barely looked at the function as I didn't consider it in scope. I ran it once to check the command and it worked. (The window is still open and I can assure you I don't use a tabstop value of 7 in my daily work. :) But now that you mention it I'm surprised it worked. Personally, I'd use let &tabstop = a:width.
    – B Layer
    Aug 5, 2021 at 12:20

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.