在 vimscript 中使用 Python:如何将值从 Python 脚本导出回 vim? [英] Using Python in vimscript: How to export a value from a python script back to vim?

查看:21
本文介绍了在 vimscript 中使用 Python:如何将值从 Python 脚本导出回 vim?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 vim 中用 Python 苦苦挣扎.
我仍然没有找到如何从 python 脚本(在 vim 函数中)将值导入到 vim p.e.

I'm struggling with Python in vim.
I still haven't found out how I can import a value from a python script (in a vim function) back to vim p.e.

function! myvimscript()

  python << endpython
    import vim, random, sys
    s = vim.eval("mylist")
    # do operation with variable "s" in python
  endpython

  " import variable "s" from above 
  " do operation with "s" in vimscript
endfunction

1) 如何在 vim 中再次使用 "s" (如何将 "s" 从 python 代码导入回 vim)?

1) How can I use "s" again in vim (how can I import "s" from the python code back to vim)?

我也不知道如何使用 vim.current.buffer 进行选择.

I can't find out as well how to use vim.current.buffer with a selection.

function! myvimscript()
  let startline = line("'<")
  let endline = line("'>")

  python << endpython
    start = vim.eval("startline")
    end = vim.eval("endline")
    cb = vim.current.buffer 
    l = cb[start:end]
  endpython
endfunction

2) 如何将动态值 "start""end" 分配给 "l"

2) How can I assign the dynamic value "start" and "end" to "l"

推荐答案

首先,请定义你的函数名以大写开头.

First of all, please define your function name starting with uppercase.

这是您的两个问题的示例.希望能帮到你:

Here is an example for your two questions. I hope it helps:

function! TestPy() range

    let startline = line("'<")
    let endline = line("'>")
    echo "vim-start:".startline . " vim-endline:".endline
python << EOF
import vim
s = "I was set in python"
vim.command("let sInVim = '%s'"% s)
start = vim.eval("startline")
end = vim.eval("endline")
print "start, end in python:%s,%s"% (start, end)
EOF
    echo sInVim
endfunction

首先我粘贴一个小测试的输出:我视觉选择了3,4,5,三行,并且:call TestPy()

first I paste the output of a small test: I visual selected 3,4,5, three lines, and :call TestPy()

我的输出:

vim-start:3 vim-endline:5
start, end in python:3,5
I was set in python

所以我稍微解释一下输出,您可能需要稍微阅读示例函数代码以理解下面的注释.

So I explain the output a bit, you may need to read the example function codes a little for understanding the comment below.

vim-start:3 vim-endline:5   #this line was printed in vim,  by vim's echo.
start, end in python:3,5    # this line was prrinted in py, using the vim var startline and endline. this answered your question two.
I was set in python         # this line was printed in vim, the variable value was set in python. it answered your question one.

我为您的函数添加了 range.因为,如果您没有它,对于每个视觉选择的行,vim 将调用您的函数一次.在我的示例中,该函数将执行 3 次 (3,4,5).与范围,它将处理视觉选择作为一个范围.对于这个例子就足够了.如果您的实际函数将执行其他操作,则可以删除 range.

I added a range for your function. because, if you don't have it, for each visual-selected line, vim will call your function once. in my example, the function will be executed 3 times (3,4,5). with range, it will handle visualselection as a range. It is sufficient for this example. If your real function will do something else, you could remove the range.

使用range,使用a:firstline 和a:lastline 效果更好.我使用 line("'<") 只是为了保持它与您的代码相同.

With range, better with a:firstline and a:lastline. I used the line("'<") just for keep it same as your codes.

EDIT 使用列表变量:

检查这个函数:

function! TestPy2()
python << EOF
import vim
s = range(10)
vim.command("let sInVim = %s"% s)
EOF
    echo type(sInVim)
    echo sInVim
endfunction

如果你调用它,输出是:

if you call it, the output is:

3
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

3"表示类型列表(检查 type() 函数).下面一行是list的字符串表示.

the "3" means type list (check type() function). and one line below is the string representation of list.

这篇关于在 vimscript 中使用 Python:如何将值从 Python 脚本导出回 vim?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆