4

According to the f-args documentation, the command line arguments passed to a user defined function, will be automatically split at white-space or tabs as the help shows:

                                    *<f-args>*
To allow commands to pass their arguments on to a user-defined function, there
is a special form <f-args> ("function args").  This splits the command
arguments at spaces and tabs, quotes each argument individually, and the
<f-args> sequence is replaced by the comma-separated list of quoted arguments.
See the Mycmd example below.  If no arguments are given <f-args> is removed.
   To embed whitespace into an argument of <f-args>, prepend a backslash.
<f-args> replaces every pair of backslashes (\\) with one backslash.  A
backslash followed by a character other than white space or a backslash
remains unmodified.  Overview:

    command        <f-args> ~
    XX ab          'ab'
    XX a\b         'a\b'
    XX a\ b        'a b'
    XX a\  b       'a ', 'b'
    XX a\\b        'a\b'
    ....

However the most basic example does not work:

function! TestFunc(...)
  echo a:0
  echo a:000
endfunction

command! -nargs=? TestFunc call TestFunc(<f-args>)

-------------------------------------------------

>  :TestFunc foo bar bla bla, fooo
>  1
>  ['foo bar bla bla, fooo']

>  :TestFunc foo\ bar
>  1
>  ['foo\ bar']

I have a bunch of arguments split by whitespaces but vim sees it as one. Why does that happen?

Side question ( can it be somehow configured to actually split arguments at comma? )

1 Answer 1

7

You specified -nargs=?.

The documentation says:

-nargs=? 0 or 1 arguments are allowed

-nargs=1 Exactly one argument is required, it includes spaces

and

Arguments are considered to be separated by (unescaped) spaces or tabs in this context, except when there is one argument, then the white space is part of the argument.

(Emphasis mine.)

If you use -nargs=* instead, you get the normal behavior.

Sign up to request clarification or add additional context in comments.

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.