1

I want to move some scripts that I have made to some sort of standalone plugin. The content would look like

myplug/
  plugin/
  mydefaultdir/

For the scripts, I require a set of files which could have any name. Those are located in mydefaultdir/. I have a function that list the content of that directory and helps use it in the ensuing script. However, I want to leave to the user the option to select an entirely different directory than the one I am providing. For that I can use

:let g:scriptdir = get(g:, 'scriptdir', "/path/to/plug/mydefaultdir")
:call myfunc(g:scriptdir)

However, I cannot be sure where my plugin is located, and hence cannot set the /path/to/plug fixed.

I suppose I could look to rtp, but even that would fail if the user decided to rename the plugin directory.

I am assuming that there are better ways to do that (using runtimepath/runtime?). But essentially; it boils down to...

How can a plugin find where it is itself located?

3

How can a plugin find where it is itself located?

You need to expand() <sfile>. Assuming your code resides in myplug/plugin/<somefile.vim>:

# the first :h is to skip the script name;
# the second is to skip "plugin/" part
let s:mydefaultdir = expand("<sfile>:h:h") . "/mydefaultdir"
...
let g:scriptdir = get(g:, 'scriptdir', s:mydefaultdir)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.