如何在 Vim 中使用 Python? [英] How can you use Python in Vim?

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

问题描述

我在 Vim 和 Python 之间浪费了很多时间.我发现手动从 Python 复制粘贴到 Vim 太慢,反之亦然.一个很好的坏例子是:

I waste a lot of time between Vim and Python. I find it too slow to manually copy-paste from Python to Vim and vice versa. A good broken example is:

%!python for i in xrange(25);打印 6*i \n

%!python for i in xrange(25); print 6*i \n

你怎么能在 Vim 中直接做这样的调整?[已解决]

How can you do such tweaks direcly in Vim? [Solved]

[澄清]我需要 Vim 的东西,比如打印序列、算术...... - 我在 Vim 中不能做的事情.

[Clarification] I need things to Vim, like printing sequences, arithmetics... - things I cannot do in Vim.

[?] 有人能详细说明这一点吗:您的脚本可以从 stdin 读取以直接对给定的行进行操作 (., %, ...)."

[?] Can someone elaborate this point: "your script can read from stdin to operate directly on the lines given (., %, ...)."

[进一步说明]

如果我想在第 4、5、6 和 7 行打印Hello",有什么问题:

If I want to print 'Hello' to lines 4,5, 6 and 7, what is wrong:

:4-7!python -c "打印'你好'"

:4-7!python -c "print 'hello'"

点.修改当前行.我可以在多行 7、32 和 99 上打印:

The dot . modifies the current line. Can I print on multiple lines 7, 32 and 99:

:7,32,99!python -c "打印'你好'"

:7,32,99!python -c "print 'hello'"

显然不起作用.怎么样?

Clearly not working. How?

推荐答案

在任何 vim 窗口中,键入如下内容:

In any of your vim windows, type something like this:

for x in range(1,10):
    print '-> %d' % x

视觉选择这两行(V 启动视觉模式),然后输入以下内容:

Visually select both of those lines (V to start visual mode), and type the following:

:!python

因为你在可视模式下按下了:",所以最终看起来像:

Because you pressed ':' in visual mode, that will end up looking like:

:'<,'>!python

按回车键,所选内容将替换为 print 语句的输出.您可以轻松地将其转换为映射:

Hit enter and the selection is replaced by the output of the print statements. You could easily turn it into a mapping:

:vnoremap <f5> :!python<CR>

这篇关于如何在 Vim 中使用 Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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