加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Generic:vim中的python命令?

发布时间:2020-12-16 01:11:48 所属栏目:安全 来源:网络整理
导读:在 vim脚本中,只要 vim是使用 python功能构建的,就可以嵌入一些python代码. function! IcecreamInitialize()python EOFclass StrawberryIcecream: def __call__(self): print('EAT ME')EOFendfunction 但是,有些人使用python3构建了vim.这为vim插件带来了一
在 vim脚本中,只要 vim是使用 python功能构建的,就可以嵌入一些python代码.
function! IcecreamInitialize()
python << EOF
class StrawberryIcecream:
    def __call__(self):
        print('EAT ME')
EOF
endfunction

但是,有些人使用python3构建了vim.这为vim插件带来了一些兼容性问题.是否有一个通用命令调用计算机上安装的任何python版本?

这个片段可以确定我们正在使用哪个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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读