微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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 = *,则会获得正常行为.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐