1

我已经尝试了所有解决方案 https://stackoverflow.com/questions/4976776/how-to-get-path-to-the-current-vimscript-being-executed/53274039#53274039 这是 7 年前发布的,没有一个是在当前(2018)MacVim(8.1)或neovim(0.3.1)中为我工作。由于我的问题一直被删除,我决定提出一个新问题。

自 7 年前的最后一个答案以来,Vim 有什么变化吗?所有这些解决方案都给了我当前文件的位置,而不是脚本的位置。SpaceVim 是否会影响这些功能的工作方式?

这是我要修复的代码:

function! TeaCodeExpand()
    <<some code>>
    echom fnamemodify(resolve(expand('<sfile>:p')), ':h')
    <<other code>>
endfunction

我有一个 echom 来显示应该是当前脚本文件的路径,但它总是返回当前编辑的文件。下一行是在 vimscript 文件上方的目录中执行 AppleScript。我可以对路径进行硬编码,一切正常。但是,我无法让它按原样工作。完整代码在这里:github.com/raguay/TeaCode-Vim-Extension

4

1 回答 1

1

<sfile>扩展插件的文件规范,必须在脚本 source 时进行扩展。见:help :<sfile>

  <sfile>    When executing a ":source" command, is replaced with the
             file name of the sourced file.
             When executing a function, is replaced with:
             "function {function-name}[{lnum}]"

如果稍后在函数中需要文件规范,则需要将其存储在(脚本本地)变量中,并从函数中引用它:

let s:scriptPath = fnamemodify(resolve(expand('<sfile>:p')), ':h')
function! TeaCodeExpand()
    <<some code>>
    echom s:scriptPath
    <<other code>>
endfunction
于 2018-11-13T09:48:58.603 回答