1

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

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
Richard Guay
  • 335
  • 2
  • 10
  • No, nothing has changed to something so basic. To better help you, please include a reproducible example that shows what path you expect and what you get instead. – Ingo Karkat Nov 13 '18 at 08:21
  • This is the code I'm trying to fix: ``` function! TeaCodeExpand() <> echom fnamemodify(resolve(expand(':p')), ':h') <> 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: https://github.com/raguay/TeaCode-Vim-Extension – Richard Guay Nov 13 '18 at 09:36
  • BTW: The echom code above was added to what is in the repository that I listed just above the call to `system`. I couldn't put the full code here due to size limits. – Richard Guay Nov 13 '18 at 09:38

1 Answers1

1

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
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324