Skip to content

Instantly share code, notes, and snippets.

@jerrymarino
Created May 22, 2018 04:44
Show Gist options
  • Save jerrymarino/9f55d181b0d65b459124ed48b61086a4 to your computer and use it in GitHub Desktop.
Save jerrymarino/9f55d181b0d65b459124ed48b61086a4 to your computer and use it in GitHub Desktop.
How to setup a vim plugin using python 2 or python 3
" Basic example of using Python with vim
" This is basic vim plugin boilerplate
let s:save_cpo = &cpo
set cpo&vim
function! s:UsingPython3()
if has('python3')
return 1
endif
return 0
endfunction
" Prefer py3
let s:using_python3 = s:UsingPython3()
let s:python_until_eof = s:using_python3 ? "python3 << EOF" : "python << EOF"
let s:python_command = s:using_python3 ? "py3 " : "py "
let s:path = fnamemodify(expand('<sfile>:p:h'), ':h')
" Setup some autocmds
autocmd BufWritePost * call s:OnBufWritePost()
autocmd CursorMoved * call s:OnCursorMoved()
autocmd CursorMovedI * call s:OnCursorMovedI()
" Run some python
function! s:Pyeval( eval_string )
if s:using_python3
return py3eval( a:eval_string )
endif
return pyeval( a:eval_string )
endfunction
function! s:SetUpPython() abort
exec s:python_until_eof
import vim
import os
print("Setting up python...")
# Directory of the plugin
plugin_dir = vim.eval('s:path')
# Add the subdir of the plugin, python
sys.path.insert(0, os.path.join(plugin_dir, 'python'))
Start()
vim.command('return 1')
EOF
endfunction
function! s:OnCursorMovedI()
endfunction
function! s:OnCursorMoved()
endfunction
function! s:OnBufWritePost()
endfunction
echom "Path " . s:path
if s:SetUpPython() != 1
echom "Setting up python failed..."
endif
" This is basic vim plugin boilerplate
let &cpo = s:save_cpo
unlet s:save_cpo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment