Setting a vim variable to a python output

北城余情 提交于 2021-02-10 15:15:26

问题


How would I do something like the following in vim:

echom "hello"
pyx print('New item')
let a = (pyx import socket; socket.gethostname())
echom a

The first and second lines work; but how to assign a variable name to a python value/output?


回答1:


Import module vim and execute vim's command let passing value from Python:

:py3 import socket, vim; hn=socket.gethostname(); vim.command('let vim_hn="'+hn+ '"')

hn=… assigns a variable in Python; 'let vim_hn="'+hn+ '"' passes its value to command let; something like let vim_hn="myhost"; vim.command() executes that let from Python.

Now you can inspect the value in vim:

:echo vim_hn



回答2:


I've experimented a few approaches

  • I prefer :let vimvar = pyxeval('PythonExpression') from vim code when possible
  • Sometimes vim.command('let vimvar = ...') makes more sense, in particular from Python code.


来源:https://stackoverflow.com/questions/62478766/setting-a-vim-variable-to-a-python-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!