Generic:vim中的python命令?

Chi*_*nke 8 python vim vim-plugin python-3.x

在vim脚本中,只要vim是使用该+python功能构建的,就可以嵌入一些python代码.

function! IcecreamInitialize()
python << EOF
class StrawberryIcecream:
    def __call__(self):
        print('EAT ME')
EOF
endfunction
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*oij 2

“heredoc”( << EOF) 语法仅限于 script :py:perl等)命令;你不能将它们与普通字符串一起使用。在 Vim 中使用行继续有点痛苦。

因此,我会将 Python 代码放在一个单独的文件中,并将其传递给:py:py3命令。

let mycode = join(readfile(expand('~/mycode.py')), "\n")

if has('python')
    execute 'py ' . mycode
elseif has('python3')
    execute 'py3 ' . mycode
else
    echoe 'Your mother was a hamster'
endif
Run Code Online (Sandbox Code Playgroud)

mycode.py脚本:

import sys
import vim
print('This is mycode', sys.version)

vim.command(':echo "Hello"')
print(vim.eval('42'))
Run Code Online (Sandbox Code Playgroud)

从Python 2开始:

('This is mycode', '2.7.10 (default, May 26 2015, 04:16:29) \n[GCC 5.1.0]')
Hello
42
Run Code Online (Sandbox Code Playgroud)

从 Python 3 开始:

This is mycode 3.4.3 (default, Mar 25 2015, 17:13:50)
[GCC 4.9.2 20150304 (prerelease)]
Hello
42
Run Code Online (Sandbox Code Playgroud)