36

I was looking around for it for quite a while.

I want to add a line to a vim plugin file that would disable it if running on unsupported version of vim.

I remember from somewhere that it goes something like that:

if version > 730
    "plugin code goes here
endif

but that fail.

GoTTimw
  • 2,330
  • 6
  • 26
  • 34
  • Why dont you do some thing like this: if version < 730 finish endif Otherwise - Add you Plugin Code. (Copied from one of the VIM files :) – hari Aug 02 '12 at 09:56

1 Answers1

60

The versioning scheme is different; Vim 7.3 is 703, not 730.

Also, for clarity, I would recommend using v:version (this is a special Vim variable).

Often, it is also better to check for the availability of features ( e.g. exists('+relativenumber')) than testing for the Vim version that introduced the feature, because Vim can be custom-compiled with different features.

Finally, plugins typically do the guard the other way around:

if v:version < 703
    finish
endif
" Plugin goes here.

And it's a good practice to combine this with an inclusion guard. This allows individual users to disable a (system-wide) installed plugin:

" Avoid installing twice or when in unsupported Vim version.
if exists('g:loaded_pluginname') || (v:version < 700)
    finish
endif
let g:loaded_pluginname = 1
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • 2
    In my case (and why I looked for this question and answer), it seems as though the `j` option to `formatoptions` was added in 7.4 and my .vimrc needs to work across that boundary for the time being. (Srsly Apple, still shipping 7.3?) – dash-tom-bang Dec 02 '15 at 08:17
  • How to show v:version value? I've tried 'set v:version?', but failed – Daniel YC Lin Feb 02 '18 at 05:01
  • 2
    @DanielYCLin `:set` is for Vim _options_; this is a special built-in _variable_. You show the value (like with any other Vimscript expression) via `:echo v:version` (or `:echomsg`). – Ingo Karkat Feb 02 '18 at 13:07
  • What's the reason for doing the guard "the other way around"? – sigvaldm Feb 09 '18 at 08:43
  • @sigvaldm: Leaving early when the version preconditions aren't met means you can focus on the plugin code after that. There's no additional indentation of your whole code. And it's also slightly more efficient for Vim's parser. – Ingo Karkat Feb 09 '18 at 08:55
  • Ah, I see! That makes sense :) Thanks! In my case I just wanted to ignore a single line in `.vimrc` whenever the version is too old. That scenario is of course slightly different. – sigvaldm Feb 09 '18 at 09:18