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


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?

1 Answer 1


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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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