问题
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