我在我的.vimrc中定义了一个python函数,以便在可视模式下在vim缓冲区上使用它。函数是vnoremap-ed to <F7>。不幸的是,所有python输出都出现在:messages of vim中。因此,我想问一下,是否可以将输出重定向到当前文件@%。也就是说,我正在寻找类似于:r!python ./some_function.py的东西,它用于调用常规python脚本并将输出传递到当前文件。我使用的函数正好为消息块添加了两行代码。
根据:help python
Vim displays all Python code output in the Vim message area. Normal
output appears as information messages, and error output appears as
error messages.
In implementation terms, this means that all output to sys.stdout
(including the output from print statements) appears as information
messages, and all output to sys.stderr (including error tracebacks)
appears as error messages.基于此,我无法想象这是如何做到的,如果有可能的话。我会感谢你的帮助。谢谢
编辑
流动的代码可以作为一个最小的例子。~/.vimrc中必须包括下列行
python << EOF
import vim
def sum_vals():
sum=sum(float_zero(v) for v in vim.eval('@"').splitlines() )
print("sum:>> %15.5E \n"%sum )
EOF
vnoremap <F7> y:python sum_vals()<Enter>
" An even simpler example
noremap <F8> :python print("hello world") <Enter>在按下<F7>时,上述函数将在可视选定的列块中提取值,并将其和打印到消息块。
按<F8>将相反地显示消息hello world。
我希望将上述函数的输出放在当前文件中,而不是:message块中。
发布于 2021-05-25 18:55:16
为了简化使用可视化模式功能,有一个外部可执行文件是有意义的。让我们创建一个,例如,第一次打开python文件时:
let g:python_summator_code =<< END
import sys
print(sum(int(line) for line in sys.stdin))
END
augroup useful_python_scripts
autocmd!
au FileType python :let g:python_summator=trim(system("mktemp"))
au FileType python :call writefile(g:python_summator_code, g:python_summator, "b")
au FileType python :vnoremap <expr><F7> "!python " . g:python_summator . " \<Enter>"
augroup END现在,您可以执行以下操作:
G 210
https://stackoverflow.com/questions/65960792
复制相似问题