Wolph

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

Exit mobile version