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

时间:2017-12-09 13:06:41

标签: 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'
    ....

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

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 个答案:

答案 0 :(得分:7)

您指定了-nargs=?

文档说:

  

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

     

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

  

参数被认为是由(未转义)空格或制表符分隔开来的   context,除非有一个参数,否则空格是其中的一部分   论证

(强调我的。)

如果您使用-nargs=*,则会获得正常行为。