Vim:如何拆分命令行参数?

ska*_*sie 3 vim

根据f-args文档,传递给用户定义函数的命令行参数将自动拆分为空格或制表符,如帮助所示:

                                    *<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'
    ....
Run Code Online (Sandbox Code Playgroud)

但是最基本的例子不起作用:

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']
Run Code Online (Sandbox Code Playgroud)

我有一堆由空格分割的参数,但是vim将其视为一个.为什么会这样?

副问题(它可以以某种方式配置为以逗号分割参数吗?)

mel*_*ene 7

你指定了-nargs=?.

文件说:

-nargs=? 允许0或1个参数

-nargs=1 只需要一个参数,它包含空格

在此上下文中,参数被认为是由(非转义)空格或制表符分隔,除非有一个参数,否则空格是参数的一部分.

(强调我的.)

如果您使用-nargs=*,则可以获得正常行为.