2017-06-14 24 views
0

對於看起來像這樣一個簡單的問題,我很抱歉。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命令中使用變量?

+0

你可以用':debug',看看什麼是真正執行 - >':調試Exmpl hello' +'s' + Enter +'s' + Enter。 .. –

回答

2

使用<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>