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将其视为一个.为什么会这样?

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

解决方法

你指定了-nargs =?.

文件说:

-nargs=? 0 or 1 arguments are allowed

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

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
.

(强调我的.)

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

相关文章

解决方案:解决linux下vim乱码的情况:(修改vimrc的内容)全...
Linuxvi/vim所有的UnixLike系统都会内建vi文书编辑器,其他的...
      vim正则匹配:空行:/^$/  /^[\t]*$/注释...
$select-editorSelectaneditor.Tochangelater,run'sele...
上次手贱忘了保存,这次就简单做个备忘吧,把踩过的坑记一下...
Linux之文本编译器小结vim的优势所有的UNIX-LIKE习通都会内置...