这个片段可以决定哪个Python版本我们正在使用和切换到它(Python代表安装的版本)。
if has('python')
command! -nargs=1 Python python <args>
elseif has('python3')
command! -nargs=1 Python python3 <args>
else
echo "Error: Requires Vim compiled with +python or +python3"
finish
endif
要加载Python代码,我们首先弄清楚它的位置(这里的同一目录Vim脚本下):
execute "Python import sys"
execute "Python sys.path.append(r'" . expand("<sfile>:p:h") . "')"
然后检查Python模块可用。如果没有,重新加载它:
Python << EOF
if 'yourModuleName' not in sys.modules:
import yourModuleName
else:
import imp
# Reload python module to avoid errors when updating plugin
yourModuleName = imp.reload(yourModuleName)
EOF
两种方式来调用它:
1.
" call the whole module
execute "Python yourModuleName"
" call a function from that module
execute "Python yourModuleName.aMethod()"
2.
" Call a method using map
vnoremap <leader> c :Python yourModuleName.aMethod()<cr>
" Call a module or method using Vim function
vnoremap <leader> c :<c-u> <SID>yourFunctionName(visualmode())<cr>
function! s:YourFunctionName(someName)
Python YourFunctionName.aMethod(a:someName)
Python YourFunctionName
endfunction
简短的答案是否定的。 Thne较长的答案是你可以抽出一些它,通过测试哪个版本的Python可用,设置一个变量'py'到'python'或'python3',然后用'exec py调用它。 'print(“吃我”)''。是的,我从来没有说过这很好。 – lcd047