Как получить путь к текущему выполнению vimscript
В моем плагине vim у меня есть два файла:
myplugin/plugin.vim
myplugin/plugin_helpers.py
Я хотел бы импортировать plugin_helpers из plugin.vim(используя поддержку vim python), поэтому я считаю, что сначала мне нужно поставить каталог моего плагина на sys.path python.
Как я могу (в vimscript) получить путь к выполняемому в настоящее время script? В python это __file__
. В рубине это __file__
. Я не мог найти ничего подобного для vim при помощи googling, может ли это быть сделано?
Примечание. Я не ищу текущий отредактированный файл ( "%: p" и друзей).
Ответы
Ответ 1
" 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
является символической ссылкой на script в репозитории git.
Ответ 2
Найдено:
let s:current_file=expand("<sfile>")
Ответ 3
Стоит отметить, что вышеупомянутое решение будет работать только вне функции.
Это не даст желаемого результата:
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:
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