vim - 如何获取当前正在执行的vim脚本的路径

标签 vim self-reference

在我的 vim 插件中,我有两个文件:

myplugin/plugin.vim
myplugin/plugin_helpers.py

我想从plugin.vim导入plugin_helpers(使用vim python支持),所以我相信我首先需要将我的插件的目录放在python的sys.path上。

我如何(在 vimscript 中)获取当前正在执行的脚本的路径?在Python中,这是__file__。在 ruby​​ 中,它是 __FILE__。我通过谷歌搜索找不到任何与 vim 类似的东西,可以吗?

注意:我不是在查找当前编辑的文件(“%:p”和 friend )。

最佳答案

" 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 存储库中脚本的符号链接(symbolic link)。

关于vim - 如何获取当前正在执行的vim脚本的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4976776/

相关文章:

vim - 在VIM命令行模式下,当前行的特殊字符/符号是什么?

c++ - 在构造函数中引用此指针

c++ - 在 C++ 中,如何制作一个可以包含相同变体 vector 的变体?

javascript - 替换新函数内的arguments.callee

php - 删除递归 PostgreSQL

vim - 覆盖 vim 命令以进行 yank

unix - 无法在 vim SunOS 中粘贴(没有注册)

vim - 将工作目录更改为当前打开的文件

vim - 如何让 VIM matchit 插件与 ColdFusion 和 HTML 一起使用?