Subash
Subash

Reputation: 7266

Check vim version and python support from a bash script

I have a bash script that is dependent on vim being at least version 7.4 and is installed with python. I need to check if the above condition matches, if not exit and ask user to update their vim.

So far all I can think of is something like below

has_vim = command -v vim >/dev/null  

if ! $has_vim; then
  echo "must have vim installed."
  exit 1
fi

// Here I want do as the following pseudo code
vim_info = $(vim --version | grep python)

// suggest me if there is another way
vim_version = // find version info from $vim_info
has_python_support = // find python support from $vim_info

if ! $vim_version >= 7.4 && ! has_python_support; then
  echo "vim version must be at least 7.4 and must be installed with python support"
fi

// everything is ok. carry on

At the moment all I can think of is checking $vim_info for expected vim version and python support.

To boil down the question into meaningful sentence:

How do I check if vim version is greater or equal to 7.4 and has python support from bash script?

Upvotes: 2

Views: 1495

Answers (3)

Ben
Ben

Reputation: 8905

If you have a running Vim instance already, or if you can create one, you can always just ask Vim itself:

$ vim --servername SOME_NAME --remote-expr "has('python')"
1
$ vim --servername SOME_NAME --remote-expr "v:version"
704

But if Vim isn't already running it's probably more straightforward to parse the --version output as suggested in other answers.

Upvotes: 0

Inian
Inian

Reputation: 85895

#!/bin/bash
has_vim=$(command -v vim >/dev/null)

if ! $has_vim; then
  echo "must have vim installed."
  exit 1
fi

# Checking the python support based on the line output received
has_python_support=$(vim --version | grep -c python) 

# Matching the decimal pattern from the first line
vim_version=$(vim --version | head -1 | grep -o '[0-9]\.[0-9]')

if [ ! $(echo "$vim_version >= 7.4" | bc -l) ] && [ ! $has_python_support ]; then
     echo "vim version must be at least 7.4 and must be installed with python support"
else
     echo "vim version is > 7.4"
fi

Should solve your problem

Upvotes: 1

eckes
eckes

Reputation: 67187

When I ask my vim for vim --version, it spits out something like this:

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Sep 16 2015 08:44:57)
Included patches: 1-872
Compiled by <[email protected]>
Huge version without GUI.  Features included (+) or not (-):
+acl             +farsi           +mouse_netterm   +syntax
+arabic          +file_in_path    +mouse_sgr       +tag_binary
+autocmd         +find_in_path    -mouse_sysmouse  +tag_old_static
-balloon_eval    +float           +mouse_urxvt     -tag_any_white
-browse          +folding         +mouse_xterm     -tcl
++builtin_terms  -footer          +multi_byte      +terminfo
+byte_offset     +fork()          +multi_lang      +termresponse
+cindent         +gettext         -mzscheme        +textobjects
-clientserver    -hangul_input    +netbeans_intg   +title
+clipboard       +iconv           +path_extra      -toolbar
+cmdline_compl   +insert_expand   +perl/dyn        +user_commands
+cmdline_hist    +jumplist        +persistent_undo +vertsplit
+cmdline_info    +keymap          +postscript      +virtualedit
+comments        +langmap         +printer         +visual
+conceal         +libcall         +profile         +visualextra
+cryptv          +linebreak       +python/dyn      +viminfo
+cscope          +lispindent      +python3/dyn     +vreplace
+cursorbind      +listcmds        +quickfix        +wildignore
+cursorshape     +localmap        +reltime         +wildmenu
+dialog_con      -lua             +rightleft       +windows
+diff            +menu            +ruby/dyn        +writebackup
+digraphs        +mksession       +scrollbind      -X11
-dnd             +modify_fname    +signs           -xfontset
-ebcdic          +mouse           +smartindent     -xim
+emacs_tags      -mouseshape      -sniff           -xsmp
+eval            +mouse_dec       +startuptime     -xterm_clipboard
+ex_extra        -mouse_gpm       +statusline      -xterm_save
+extra_search    -mouse_jsbterm   -sun_workshop    -xpm

So, parsing the output would be a good bet here.

Find the version number and store it in VIMVERSION:

VIMVERSION=$(vim --version | head -1 | cut -d ' ' -f 5)

From here, check In bash shell script how do I convert a string to an number for how to compare the string result against your minimum needed 7.4.

Check for Python support (HAS_PYTHON will be 0 if Python is not available):

HAS_PYTHON=$(vim --version | grep -c '+python')

Check explicitly for Python 3 (again, HAS_PYTHON3 will be 0 if Python 3 is not available):

HAS_PYTHON3=$(vim --version | grep -c '+python3')

This might be a bit rough but I think you get the idea.

Upvotes: 5

Related Questions