在Mac OS X上删除Vim中凌乱的前导空格

时间:2009-07-11 23:23:29

标签: macos vim

我正在从网站matplotlib复制代码并粘贴到Mac OS X终端上的Vim编辑器中:

pylab_examples example code: ellipse_demo.py

虽然这在BBEdit中可以正常工作:

`from pylab import figure, show, rand
from matplotlib.patches import Ellipse

NUM = 250

ells = [Ellipse(xy=rand(2)*10, width=rand(), height=rand(), angle=rand()*360)
        for i in xrange(NUM)]

fig = figure()
ax = fig.add_subplot(111, aspect='equal')
for e in ells:
    ax.add_artist(e)
    e.set_clip_box(ax.bbox)
    e.set_alpha(rand())
    e.set_facecolor(rand(3))

ax.set_xlim(0, 10)
ax.set_ylim(0, 10)

show()

也就是说,所有代码都已正确对齐。在Vim中它看起来像这样:

from pylab import figure, show, rand
from matplotlib.patches import Ellipse

NUM = 250

ells = [Ellipse(xy=rand(2)*10, width=rand(), height=rand(), angle=rand()*360)
        for i in xrange(NUM)]

            fig = figure()
            ax = fig.add_subplot(111, aspect='equal')
            for e in ells:
                    ax.add_artist(e)
                        e.set_clip_box(ax.bbox)
                            e.set_alpha(rand())
                                e.set_facecolor(rand(3))

                                ax.set_xlim(0, 10)
                                ax.set_ylim(0, 10)

                                show()

如何解决这种烦人的情况?它是否与Mac上的不同回车/换行约定有关?

5 个答案:

答案 0 :(得分:17)

在粘贴文本之前使用:set paste命令。这会关闭autoindent以及其他可能干扰粘贴的其他事情。要恢复正常操作,请使用:set nopaste

答案 1 :(得分:2)

或者使用:r!pbpaste - 在当前行下插入来自pbpaste命令(恰好是当前粘贴缓冲区)的输出;没有必要弄乱模式等等。

答案 2 :(得分:1)

如果必须,您可以使用cat(1)

$ cat > newfile.py
Paste the code here, then press Ctrl-D for EOF.
Make sure to type EOF on an otherwise empty line
or bad things will happen to your children.
$ vi newfile.py

应该工作。

答案 3 :(得分:0)

除了已经提到的:set paste命令外,您始终可以"+p将剪贴板的内容粘贴到当前位置。我没有在Mac OS X上使用过Vim,但我确信它仍然有效。在Linux上,选择剪贴板为"*p,复制粘贴剪贴板为"+p;在Windows上,它们都指向系统剪贴板。 "+p起初是一个奇怪的命令,但是一旦你习惯使用这种类型的命令,它就变得非常快。或者,您可以使用:put +执行相同的操作。

:help quote
:help put
:help :put
:help registers

答案 4 :(得分:-1)

如果您使用的是emacs,那么M-x mark-whole-bufferM-x indent-region就会很简单。