Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compatible way to use either :py OR :py3 in vim?

In my .vimrc and my vim plugin UltiSnips I've got a lot of code that looks like that

:py << EOF
print("Hi")
EOF

Now, I want to check if python3 is compiled into Vim via has("python3") and then use :py3 instead of :py. Keeping the python code compatible between python 2 and 3 is not the issue - the issue is to tell vim to use :py3 if is available and :py otherwise.

Has someone a good idea?

like image 310
SirVer Avatar asked Jan 10 '12 14:01

SirVer


1 Answers

You can take advantage on the fact that user defined commands in vim are simply place-in-patter-and-eval statements, and write:

if has("python3")
    command! -nargs=1 Py py3 <args>
else
    command! -nargs=1 Py py <args>
endif

Then you can use :Py to run python commands the same way as you regularly use :py or :py3.

like image 163
Idan Arye Avatar answered Oct 08 '22 00:10

Idan Arye