Vimscript:在函数调用之前将变量解析为值
问题描述:
对于看起来像这样一个简单的问题,我很抱歉。Vimscript:在函数调用之前将变量解析为值
我有一个示例命令定义如下:
command! -nargs=+ Exmpl call Example(<f-args>)
function! Example(name)
echo a:name
endfunc
这是如下调用:
Exmpl 'hello'
,但它似乎从字面上解释,而不解析给定的参数。所以:
let hello = 'world'
Exmpl hello
不按预期打印world
。
这显然是明显的,我在the literature找不到。我已经尝试添加各种转义字符,如&
,但随后的参数始终被视为裸露的单词。
如何在自定义Vim命令中使用变量?
答
使用<args>
而不是<f-args>
做你正在问的。当被调用时
该命令定义扩展任何提供的参数
command! -nargs=+ Example call Example(<args>)
function! Example(name)
echo a:name
endfunc
let hello = 'world'
Exmpl hello
输出
world
参考
:help command
然后搜索帮助文本:/
标签<args>
你可以用':debug',看看什么是真正执行 - >':调试Exmpl hello' +'s' + Enter +'s' + Enter。 .. –