I wrote a function to get the full path of the current file under the cursor
nmap <F12> :echo GetFullPath()<cr>
function! GetFullPath()
let currentFile=expand("<cfile>")
let afterChangeSlash=substitute(currentFile,"/","\\","g")
let fullPath="e:\\Test\\".afterChangeSlash
return fullPath
endfunction
When I call the function after the :echo command, I get the expected result,like:
:echo GetFullPath()
e:\Test\test.h
However,When I call it after the :e(edit) command:
:e GetFullPath()
Vim just create a new file named GetFullPath()
Why the command :e will treat a function call literally while the command :echo won’t?
You can use
:executeto build your ex command string and execute it:Or use the `=` syntax to expand a Vim expression:
If you check the help for
:editand:echo, you’ll notice that the former expects its argument to be the file name (literally), while:echoexpects an expression which will be evaluated.