我从 natw 的 vimrc 中获取了这个(我认为......也许是 sontek),但它应该可以解决问题,只要您当前安装的 Python 可以找到您的包。这使您可以使用 gf,但也可以设置搜索这些文件以进行自动完成。注意py <<EOF 部分,它启动了一个用 Python 解释的部分。这意味着您必须在 vim 中安装 python 解释器才能使用它。
function! LoadPythonPath()
py <<EOF
# load PYTHONPATH into vim, this lets you hover over a module name
# and type 'gf' (for goto file) and open that file in vim. Useful
# and easier than rope for simple tasks
import os.path
import sys
import vim
for p in sys.path:
if os.path.isdir(p):
vim.command(r"set path+=%s" % (p.replace(" ", r"\ ")))
EOF
endfunction
顺便说一句,我不喜欢自动加载,所以我将它设置为一个函数,当我调用它/第一次输入 Python 文档时,它可以智能地加载/卸载。我在前面的函数中添加了let g:PythonPathLoaded=1。
function! GetPythonPath()
if !exists("g:PythonPathLoaded")
call LoadPythonPath()
return
elseif g:PythonPathLoaded
return
else
call LoadPythonPath()
endif
endfunction
我也有一个卸载功能......虽然我不确定这是否会产生巨大的影响。
function! UnloadPythonPath()
py <<EOF
# load PYTHONPATH into vim, this lets you hover over a module name
# and type 'gf' (for goto file) and open that file in vim. Useful
# and easier than rope for simple tasks
for p in sys.path:
if os.path.isdir(p):
vim.command(r"set path-=%s" % (p.replace(" ", r"\ ")))
EOF
let g:PythonPathLoaded = 0
endfunction
希望这会有所帮助!另外,一个额外的好处是,无论您是否使用 virtualenv,这都会加载您的包(因为我相信它运行目前设置为“python”的任何内容)。