Checking for Python version and Vim version in your .vimrc

Recently I’ve had to adjust a bunch of my dotfiles to support some old (Centos 5) systems which means that I am using a Vim that has Python 2.4 build in… needless to say, it breaks some of my dotfiles 😉

So here’s some tips on patching Vim version issues.

First, checking if you have Python in your Vim and which version you are using. It returns a version similar to how Vim does it with it’s version. So 204 is the result for Python 2.4, 207 for Python 2.7 and so on.
[vim]
“”””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””
” Check python version if available
“”””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””””
if has(“python”)
python import vim; from sys import version_info as v; vim.command(‘let python_version=%d’ % (v[0] * 100 + v[1]))
else
let python_version=0
endif
[/vim]

Now we can make plugins/bundles dependend on versions:
[vim]
if python_version >= 205
” Ultisnips requires Vim 2.5 or higher due to the with_statement
Bundle ‘SirVer/ultisnips’
else
Bundle “MarcWeber/vim-addon-mw-utils”
Bundle “tomtom/tlib_vim”
Bundle “garbas/vim-snipmate”
endif
[/vim]

And checking for the Vim version to see if features are available:
[vim]
if version >= 703
set undofile
set undodir=~/.vim/undo
set undolevels=10000

call system(‘mkdir ‘ . expand(‘~/.vim/undo’))
endif
[/vim]

That’s it, the examples can be found in my Vim config: https://github.com/WoLpH/dotfiles/blob/master/_vimrc

Bookmark and Share

Tags:

About Rick van Hattem

Rick van Hattem is a Dutch Internet entrepreneur and co-founder of Fashiolista.com
No comments yet.

Leave a Reply