VIM设置python3支持和检测python version

不多说,直接下上我的代码
首先,在windows的gvim里,现在发现通过set pythonthreedll=, set pythonhome=不能设置pythonthreedllpythonhome,放狗后建议下面的设置方法

let &pythonthreedll='C:\Anaconda3\python37.dll'
let &pythonthreehome='C:\Anaconda3'

然后检测系统python版本、位置,返回的python版本是一个小数,如3.73, 2.717

" python support
function! GetPyxVerion() abort
   try
       let s:temp = execute('pyx print(sys.version)')[1:6]
       let s:python_version = str2float(s:temp[0:2])
       if s:temp[5] == ' '
           return s:python_version + str2float(s:temp[4])/100
       else
           return s:python_version + str2float(s:temp[4:5])/1000
       endif
   catch
       return 0
   endtry
endfunction
if !exists("g:python3_host_prog")
   let g:python3_host_prog = ""
endif
if !exists("g:python_host_prog")
   let g:python_host_prog = ""
endif
let g:python_version  = 0
let g:pygments_import = 0
let g:neovim_import   = 0
let g:python_exepath  = ""
let g:python_version = GetPyxVerion()
if g:python_version > 3
   if  g:python3_host_prog == ''
       if WINDOWS()
           try
               let g:python3_host_prog = exepath('python3')
           catch
               let g:python3_host_prog = exepath('python')
           endtry
       elseif has('nvim') || v:version >= 800
           let g:python3_host_prog = exepath('python3')
       else
           let g:python3_host_prog = system('which python3')
       endif
   endif
elseif g:python_version > 2
   if  g:python_host_prog == ''
       if WINDOWS()
           let g:python_host_prog = exepath('python2')
       elseif has('nvim') || v:version >= 800
           let g:python3_host_prog = exepath('python')
       else
           let g:python3_host_prog = system('which python')
       endif
   endif
endif
if g:python3_host_prog != ""
py3 << pythonEOF
try:
   import vim
   import pygments
except Exception:
   pass
else:
   vim.command('let g:pygments_import = 1')
try:
   import neovim
except Exception:
   pass
else:
   vim.command('let g:neovim_import = 1')
pythonEOF
elseif g:python_host_prog != ""
py << pythonEOF
try:
   import vim
   import pygments
except Exception:
   pass
else:
   vim.command('let g:pygments_import = 1')
pythonEOF
endif

有些vim插件如defx, denite, deoplete是需要python3.6.1+, 那在vim配置里只要 g:python_version >3.6即可。

你可能感兴趣的:(VIM设置python3支持和检测python version)