Quantcast

Python 2/3 scripting in vimscript

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Python 2/3 scripting in vimscript

Jonathan Crall
I've recently made the jump to python3 and I went to recompile vim with +python3 instead of +python. When I did this I noticed that all my python blocks in my .vimrc were failing.

Example of a python block:
```
python << endpython
# <some python code>
endpython
```


I was able to replace all of these with
```
python3 << EOF
# <some python code>
EOF
```

but all of my code in these blocks is 2/3 compatible and is critical for my .vimrc to function properly. Furthermore, I have several external vimscripts that use python blocks and having to change those is a pain.

Is there a way to dynamically define which python script to use?

```
if has('python3')
    #define pythonX python3
else
    #define pythonX python2
endif

pythonX << EOF
# <some python code>
EOF  
```

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Python 2/3 scripting in vimscript

Nikolay Aleksandrovich Pavlov
2017-02-07 17:27 GMT+03:00 Jonathan Crall <[hidden email]>:
> I've recently made the jump to python3 and I went to recompile vim with +python3 instead of +python. When I did this I noticed that all my python blocks in my .vimrc were failing.

You can compile with both +python/dyn and +python3/dyn

>
> Example of a python block:
> ```
> python << endpython
> # <some python code>
> endpython
> ```
>
>
> I was able to replace all of these with
> ```
> python3 << EOF
> # <some python code>
> EOF
> ```

It is bad idea to have such blocks: every variable defined inside is
global. Use modules.

>
> but all of my code in these blocks is 2/3 compatible and is critical for my .vimrc to function properly. Furthermore, I have several external vimscripts that use python blocks and having to change those is a pain.
>
> Is there a way to dynamically define which python script to use?
>
> ```
> if has('python3')
>     #define pythonX python3
> else
>     #define pythonX python2
> endif
>
> pythonX << EOF
> # <some python code>
> EOF
> ```

With modules it is really easy to write

    execute g:myplugin_python 'import myplugin; myplugin.do_stuff()'

where `g:myplugin_python` is either `python` or `python3`.

Recently a patch was made to add `:pythonx` command which may use
either version of Python, but I would not recommend using it in
plugins (except that it is a good idea to check for it when setting
default value of `g:myplugin_python`) for at least a year: Vim
versions without :pythonx are going away rather slowly.

>
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> For more options, visit https://groups.google.com/d/optout.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...