tags:

views:

353

answers:

5

Hi,

I am copying code from website matplotlib and pasting into the vim editor in a terminal on OSX:

[(http://matplotlib.sourceforge.net/examples/pylab_examples/ellipse_demo.html)][1]

While this works fine in 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()

ie all the code is properly aligned, in VIM it looks like this:

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()

Anybody know how to fix this annoying situation? Does it have something to do with the different carriage return / line feed conventions on the Mac? Thanks.

+10  A: 

Use the :set paste command before pasting the text. This turns off autoindent plus various other things that can interfere with pasting. To restore normal operation, use :set nopaste.

Greg Hewgill
tried it Greg - still doesn't work
Thomas Browne
Well I'm not sure what to suggest then. What you describe is precisely the problem that the `:set paste` command is designed to solve. I use it whenever I am faced with the same problem and it works for me.
Greg Hewgill
off that previous comment of mine. Clearly too late at night here in London! Got it working. Thanks Greg nice one.
Thomas Browne
A: 

If you were using emacs, it would be a simple matter of M-x mark-whole-buffer and M-x indent-region.

jrockway
Contrary to popular believe, "Use something else" is rarely the solution.
Matthew Scharley
Thanks jrockway haha. Sadly the indents get incrementally larger on each line so doesn't help, or I would have used Vim's < command ;)
Thomas Browne
And, as we see, since he's using Vim, it's as easy as `:set paste`.
Telemachus
I missed the part about pasting. In that case, it Just Works.
jrockway
(In the case of pasting the second example into emacs, only the last three lines are left with incorrect indentation.)
jrockway
Vim also has an auto-indent function (gg=G for example), but this is not necessarily helpful for python code as it is impossible for emacs or Vim to know the indent level for a line of python code (unless it's the line immediately after a line ending ':').
Al
+1  A: 

If you have to, you could just use 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

Should work.

Chris Lutz
yep works perfectly too.
Thomas Browne
I recommend the :set paste version, especially now that I know about it, but at the time I started posting I didn't know it worked for you, so I posted it just in case.
Chris Lutz
I always used this method in the past, but after learning about "set paste" I'll likely switch too.
James Thompson
+1  A: 

Or use :r!pbpaste -- that inserts output from the pbpaste command (which happens to be the current paste buffer) under the current line; no need to mess about with modes and such.

Gordon Davisson
Using an external command is overkill as there is :put and "+p that do exactly the same thing.
Al
Not in OS X; see my comment on your answer.
Gordon Davisson
A: 

As well as the :set paste command already mentioned, you can always do "+p to paste the contents of the clipboard into the current location. I've not used Vim on Mac OS X, but I'm sure this is still valid. On Linux, there's "*p for the selection clipboard and "+p for the copy-and-paste clipboard; on Windows, they both point to the system clipboard. "+p is a bit of an odd command at first, but once you're used to using this type of command, it becomes very quick. Alternatively you could use :put + to do the same thing.

:help quote
:help put
:help :put
:help registers
Al
On OS X this doesn't seem to work; when I tested it, the `+` register didn't pull from the system clipboard, just from vi's internal register. I tried both `vi` and `vim`, running in both xterm and Apple's native Terminal.app; `:r!pbpaste` works, vi's register integration doesn't.
Gordon Davisson