I've tried all the solutionshttps://stackoverflow.com/questions/4976776/how-to-get-path-to-the-current-vimscript-being-executed/53274039#53274039 which was posted 7 years ago and none are working for me in the current (2018) MacVim (8.1) or neovim (0.3.1). Since my question there keeps being deleted, I decided to ask in a new question.
Has something changed in Vim since the last answer 7 years ago? All of these solutions are giving me the current file's location and not the location of the script. Is SpaceVim effecting how these functions work?
This is the code I'm trying to fix:
function! TeaCodeExpand()
<<some code>>
echom fnamemodify(resolve(expand('<sfile>:p')), ':h')
<<other code>>
endfunction
I have an echom to show the path that should be the current script file, but it always returns the currently edited file. The next line is to execute an AppleScript in the directory above the vimscript file. I can hard code the path and everything works fine. But, I can't get it to work as is. The full code is here: github.com/raguay/TeaCode-Vim-Extension
To have the <sfile>
expand to the plugin's filespec, the expansion has to happen when the script is sourced. See :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}]"
If you need the filespec later in a function, you need to store it in a (script-local) variable, and reference that from the function:
let s:scriptPath = fnamemodify(resolve(expand('<sfile>:p')), ':h')
function! TeaCodeExpand()
<<some code>>
echom s:scriptPath
<<other code>>
endfunction