如何获取函数';s Vim中:e命令后的返回值

如何获取函数';s Vim中:e命令后的返回值,vim,Vim,我编写了一个函数来获取光标下当前文件的完整路径 nmap <F12> :echo GetFullPath()<cr> function! GetFullPath() let currentFile=expand("<cfile>") let afterChangeSlash=substitute(currentFile,"/","\\","g") let fullPath="e:\\Test\\".afterChangeSlash

我编写了一个函数来获取光标下当前文件的完整路径

nmap <F12> :echo GetFullPath()<cr>
function! GetFullPath()
    let currentFile=expand("<cfile>")
    let afterChangeSlash=substitute(currentFile,"/","\\","g")
    let fullPath="e:\\Test\\".afterChangeSlash
    return fullPath
endfunction
e:\Test\Test.h

但是,当我在:e(编辑)命令后调用它时:

Vim只需创建一个名为GetFullPath()的新文件

为什么命令:e会逐字处理函数调用,而命令:echo不会?

您可以使用它来构建ex命令字符串并执行它:

:exe "e ".GetFullPath()
或使用以下语法展开Vim表达式:

:e `=GetFullPath()`

如果查看
:edit
:echo
的帮助,您会注意到前者希望其参数是文件名(字面意思),而
:echo
希望得到一个将被计算的表达式。

一些ex命令希望得到一个表达式,而另一些命令希望得到一个字符串。要使您的案例发挥作用,请使用exec:

nmap <F12> :exec 'e ' . GetFullPath()
nmap:exec'e'。GetFullPath()
:e `=GetFullPath()`
nmap <F12> :exec 'e ' . GetFullPath()