如何获取当前正在执行的vim脚本的路径
在我的vim插件中,我有两个文件:
myplugin/plugin.vim
myplugin/plugin_helpers.py
我想从plugin.vim导入plugin_helpers(使用vim python支持),所以我相信我首先需要将我的插件的目录放在python的sys.path上。
我如何(在 vimscript 中)获取当前正在执行的脚本的路径?在Python中,这是__file__
。在 ruby 中,它是 __FILE__
。我通过谷歌搜索找不到类似 vim 的东西,可以吗?
注意:我不是在寻找当前编辑的文件(“%:p”和朋友)。
In my vim plugin, I have two files:
myplugin/plugin.vim
myplugin/plugin_helpers.py
I would like to import plugin_helpers from plugin.vim (using the vim python support), so I believe I first need to put the directory of my plugin on python's sys.path.
How can I (in vimscript) get the path to the currently executing script? In python, this is __file__
. In ruby, it's __FILE__
. I couldn't find anything similar for vim by googling, can it be done?
Note: I am not looking for the currently edited file ("%:p" and friends).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我经常使用最后一个,因为我的
~/.vimrc
是 git 存储库中脚本的符号链接。I use that last one often because my
~/.vimrc
is a symbolic link to a script in a git repository.找到了:
Found it:
值得一提的是,上述解决方案只能在函数之外工作。
这不会给出预期的结果:
但这会:
这是OP原始问题的完整解决方案:
It is worth mentioning that the above solution will only work outside of a function.
This will not give the desired result:
But this will:
Here's a full solution to OP's original question:
如果您确实想获取函数内的脚本路径(这就是我想要的),您仍然可以使用
的第二个语义,或其等效的<
。expand()
内的;stack>但是,您可能不想在插件中使用它,因为您可以使用此
relative#path#to#plugin#root#script
表示法在插件中使用自动加载函数。我将其用于采购目的:
然后您可以在任何脚本内
SourceLocal
来获取与其相关的另一个脚本。If you really want to get the script path inside a function (which is what I'd like to), you can still use
<sfile>
's second semantic, or its equivalent<stack>
insideexpand()
.However you possibly don't want to use it in a plugin, as you can use autoload functions in plugin, using this
relative#path#to#plugin#root#script
notation.I use this for sourcing purpose:
Then you can
SourceLocal
inside any script to source another script relative to it.