如何获得正在执行的当前vimscript的path
在我的vim插件中,我有两个文件:
myplugin/plugin.vim myplugin/plugin_helpers.py
我想从plugin.vim(使用vim python支持)导入plugin_helpers,所以我相信我首先需要把我的插件的目录放在python的sys.path中。
我如何(在vimscript中)获取当前正在执行的脚本的path? 在Python中,这是__file__
。 在ruby中,它是__FILE__
。 我无法find任何类似的谷歌search引擎,可以做到这一点?
注意:我不查找当前编辑的文件(“%:p”和朋友)。
" 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仓库脚本的符号链接。
find了:
let s:current_file=expand("<sfile>")
值得一提的是,上面的解决scheme只能在一个函数之外工作。
这不会给出预期的结果:
function! MyFunction() let s:current_file=expand('<sfile>:p:h') echom s:current_file endfunction
但是这将会:
let s:current_file=expand('<sfile>') function! MyFunction() echom s:current_file endfunction
以下是OP最初的问题的完整解决scheme:
let s:path = expand('<sfile>:p:h') function! MyPythonFunction() import sys import os script_path = vim.eval('s:path') lib_path = os.path.join(script_path, '.') sys.path.insert(0, lib_path) import vim import plugin_helpers plugin_helpers.do_some_cool_stuff_here() vim.command("badd %(result)s" % {'result':plugin_helpers.get_result()}) EOF endfunction