开发者

python script to send commands to vim (mvim)

开发者 https://www.devze.com 2023-03-03 11:38 出处:网络
I wrote a very simple vim plugin and python script trying to test some communication between the two. My vim-script looks like this:

I wrote a very simple vim plugin and python script trying to test some communication between the two. My vim-script looks like this:

function! HelloWorld()
    silent :!python helloworld.py
endf

nmap <C-P> :call HelloWorld()<CR>

then my python script looks like this:

import os;

os.system( 'mvim --servername VIM -u NONE -U NONE --remote-send \"<C-\\\\><C-N>:echo \'Hello World!\'<CR>\"' )

If I am in vim and press , use the ":call HelloWorld()" command, or just type ":!python helloworld.py" from the same or another mvim or vim instance, nothing happens. However, if I call the script from the command line separately, mvim responds appropriately: shows "Hello World!" along the bottom.

Does anyone have an开发者_高级运维y idea why it is not working when called from vim?


Try replacing

silent :!python helloworld.py

with

silent :!(sleep 0.5s && python helloworld.py) &
redraw!

(the point is in returning to vim before remote command arrives). If it works, then the problem is in processing remote commands while receiving shell output. You can also try another workarounds:

call system('python helloworld.py')

,

call system('python helloworld.py &')

and

pyfile helloworld.py

(Note that the last one requires vim compiled with +python feature and also alters the state of python interpreter used by vim).

By the way, use system() call instead of ! when you don't want to see the script output. Also use redraw! after silent !.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号