Within a vim script it is possible to embed some python code, as long as vim is built with the +python feature.
function! IcecreamInitialize()
python << EOF
class StrawberryIcecream:
def __call__(self):
print('EAT ME')
EOF
endfunction
However, some people have vim built with +python3 instead. This brings up some compatibility issues for vim plugins. Is there a generic command which calls whichever python version is installed on the computer?
The "heredoc" (
<< EOF) syntax is limited only to the script:py,:perl, etc.) commands; you can't use them with normal strings. And using line-continuation in Vim is a bit of a pain.For this reason, I would put the Python code in a separate file, and pass this to the
:pyor:py3commands.And the
mycode.pyscript:From Python 2:
And from Python 3: