Within some run time files I would like to source a file relative to the $MYVIMRC file. My best efforts have lead me to this (which does not work):
source expand("$MYVIMRC:p:h") . "/vimrc/env.vim")
Your best effort:
source expand("$MYVIMRC:p:h") . "/vimrc/env.vim")
has issues:
source
command doesn't work with "strings"
)
You can try using :execute
statement with composed source
:
:exe "source " . fnamemodify("$MYVIMRC", ":p:h") . "/vimrc/env.vim"
Note that expand("$MYVIMRC:p:h")
will not work as expected, but fnamemodify
will.
Another option I personally use is:
source <sfile>:h/statusline.vim
where <sfile>
when executing a ":source" command, is replaced with the file name of the sourced file.
fnamemodify(expand("$MYVIMRC"), ":p:h") to get this to work ... or fnamemodify($MYVIMRC, ":p:h")
A possible solution:
execute printf('source %s/vimrc/env.vim', fnamemodify($MYVIMRC, ':h'))
:exe "source " . expand("$MYVIMRC:p:h") . "/vimrc/env.vim"
might work, but I am not sure.