如何获取当前正在执行的vimscript的路径 [英] How to get path to the current vimscript being executed
问题描述
在我的 vim 插件中,我有两个文件:
In my vim plugin, I have two files:
myplugin/plugin.vim
myplugin/plugin_helpers.py
我想从 plugin.vim 导入 plugin_helpers(使用 vim python 支持),所以我相信我首先需要将我的插件目录放在 python 的 sys.path 上.
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.
我如何(在 vimscript 中)获取当前正在执行的脚本的路径?在python中,这是__file__
.在 ruby 中,它是 __FILE__
.我通过谷歌搜索找不到与 vim 类似的东西,可以吗?
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?
注意:我不是在寻找当前编辑的文件(%:p"和朋友).
Note: I am not looking for the currently edited file ("%:p" and friends).
推荐答案
" Relative path of script file:
let s:path = expand('<sfile>')
" Absolute path of script file:
let s:path = expand('<sfile>:p')
" Absolute path of script file with symbolic links resolved:
let s:path = resolve(expand('<sfile>:p'))
" Folder in which script resides: (not safe for symlinks)
let s:path = expand('<sfile>:p:h')
" If you're using a symlink to your script, but your resources are in
" the same directory as the actual script, you'll need to do this:
" 1: Get the absolute path of the script
" 2: Resolve all symbolic links
" 3: Get the folder of the resolved absolute file
let s:path = fnamemodify(resolve(expand('<sfile>:p')), ':h')
我经常使用最后一个,因为我的 ~/.vimrc
是指向 git 存储库中脚本的符号链接.
I use that last one often because my ~/.vimrc
is a symbolic link to a script in a git repository.
这篇关于如何获取当前正在执行的vimscript的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!