Generic :python command in vim? -


within vim script possible embed python code, long vim built +python feature.

function! icecreaminitialize() python << eof class strawberryicecream:     def __call__(self):         print('eat me') eof endfunction 

however, people have vim built +python3 instead. brings compatibility issues vim plugins. there generic command calls whichever python version installed on computer?

this snippet determine python version we're using , switch it(python stands version installed).

if has('python')     command! -nargs=1 python python <args> elseif has('python3')     command! -nargs=1 python python3 <args> else     echo "error: requires vim compiled +python or +python3"     finish endif 

to load python code, first figure out location(here under same directory vim script):

execute "python import sys" execute "python sys.path.append(r'" . expand("<sfile>:p:h") . "')" 

then check if python module available. if not, reload it:

python << eof if 'yourmodulename' not in sys.modules:     import yourmodulename else:     import imp     # reload python module avoid errors when updating plugin     yourmodulename = imp.reload(yourmodulename) eof 

two ways call it:
1.

" call whole module execute "python yourmodulename"  " call function module execute "python yourmodulename.amethod()" 

2.

" call method using map  vnoremap <leader> c :python yourmodulename.amethod()<cr>   " call module or method using vim function vnoremap <leader> c :<c-u> <sid>yourfunctionname(visualmode())<cr> function! s:yourfunctionname(somename)     python yourfunctionname.amethod(a:somename)     python yourfunctionname endfunction 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -