vim - 使用列表或 splat 参数调用函数

标签 vim

有没有办法用参数列表调用 vim 函数。我的参数列表来自另一个函数中的可选 splat 参数,我需要一种方法将这些参数传递给目标函数。

目标函数是,

function! run_hello(cmd, ...)
  echo 'run_hello'
  echo a:cmd
  echo a:000
endfunction

将调用 run_hello 的函数是,
function! hello(...)
  call run_hello('foo', the splats here)
endfunction

它将像这样调用,具有不同的参数。
call hello('lorem', 'ipsum', 'dolor')

我目前正在使用 hello(arglist)并通过 a:000向前列出。但是我想知道是否可以使用列表作为参数调用函数,然后成为常规参数列表。

类似于 JavaScript 的东西,
foo.apply(this, ['a', 'b', 'c']

谢谢。

最佳答案

相当于 JavaScript 的 apply()call()在 Vimscript 中:

function! hello(...)
    call call('run_hello', ['foo'] + a:000)
endfunction

这不是错别字:你 :call名为 call() 的函数.

关于vim - 使用列表或 splat 参数调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18249792/

相关文章:

regex - 我可以在 VIM 或 Perl 的单个正则表达式中替换多个项目吗?

python - ctags 不递归搜索

vim - Vim 的 Razor .cshtml 语法突出显示?

更改颜色方案后 Vim 多个光标不可见

vim - 改进 vimdiff 语法高亮

vim - Vim中的:update和:w有什么区别?

c - vim 技巧包 : how to find active C preprocessing conditionals

python - 在 Vim 中使用 Python 2 和 3(在 Windows 上)

Vim 语法文件...试图理解 "contains"

python - 有没有办法让 vim 自动换行 79 个字符的 python 字符串?