Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: How to split command line arguments?

Tags:

vim

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? )

like image 726
skamsie Avatar asked Sep 02 '25 16:09

skamsie


1 Answers

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.

like image 130
melpomene Avatar answered Sep 04 '25 15:09

melpomene