gpt4 book ai didi

python - vim:使用选定的行作为 python 函数的输入

转载 作者:太空宇宙 更新时间:2023-11-03 11:06:39 24 4
gpt4 key购买 nike

我是 vim 的新手。我想要实现的是 vim 将在我的文本文件中返回 python 函数的输出。这个想法是我选择了一部分文本并按 F5 并获得输出。复杂的是这个函数是库的一部分,我必须导入它。这个我试过了

command MyPythonFunction execute "!python from Bio.Seq import reverse_complement; reverse_complement(%)"
map <F5> :MyPythonFunction<CR>

我得到“不允许范围”

最佳答案

选定的行被传递到命令的标准输入。所以使用 sys.stdin.read() 读取它

fun! ReverseComplement()
exec ":'<,'>! python -c \"import sys, Bio.Seq; print Bio.Seq.reverse_complement(sys.stdin.read().rstrip())\""
endfun

vnoremap <F5> :call ReverseComplement()<CR>

编辑

传递一行的一部分:

vnoremap <F5> d:let @a=system('python -c "import sys, Bio.Seq; sys.stdout.write(Bio.Seq.reverse_complement(\"' . @" . '\"))"')<CR>"aP
  • d -> 删除所选部分。副作用:寄存器 " 的内容改变了。
  • @":寄存器"的内容(剪切内容)
  • .:连接字符串的二元运算符。
  • let @a = system(..): 执行外部命令,并将其输出保存到a寄存器。
  • "aP: 在当前光标位置之前粘贴a寄存器的内容。

关于python - vim:使用选定的行作为 python 函数的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17500516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com