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

3
根据 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'
    ....

然而,最基本的示例并不起作用:

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']

我有一堆用空格分隔的参数,但vim只将它视为一个参数。为什么会这样呢?

另外一个问题是(是否可以配置为在逗号处分割参数?)

1个回答

7
您指定了-nargs=?
文档说明如下:

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

-nargs=1 必须有一个参数,其中包括空格

并且

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

(强调是我自己加的。)
如果您使用-nargs=*,则会获得正常行为。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接