views:

782

answers:

3

I guess other editors are smart enough to turn that stuff off for pasting but when using vim in a terminal it can't distinguish between pasting and actual typing.

What kinds of solutions or workarounds do you have for this?

Added: there's also a setting that makes comments automatically continue on the next line. The indenting at least doesn't change the semantics of the code but the auto comment continuation really screws things up. Come to think of it, I should just turn that off altogether -- anyone know what that option is called?

+12  A: 

:set paste

J.J.
:set nopaste - to turn indenting back on
J.J.
oh ho, thanks. so you have to remember to set it and unset it before and after pasting? I guess nothing more automatic is possible without a programmatic way to distinguish pasting and typing.
dreeves
You could use Key Mapping to bind it to a set of keys. But I have never tried this. http://www.shlomifish.org/lecture/Vim/beginners/slides/slide30.html
J.J.
Cool, want to incorporate that into your answer? And maybe add something about the feature that automatically continues commenting, if you know what I'm talking about there.
dreeves
@dreeves: automatically continued commenting is also disabled by :set paste
Dan Fitch
Good point, although in that case there isn't a simple ex-post fix like there is with indenting so I think I'd rather just turn that feature off.
dreeves
@dreeves: :set fo-=r:he fo-table for more info on all the stuff you can set
Dan Fitch
(doh, forgot no linebreaks in comments -- :he is a separate command obviously... and now I will stop derailing this comment thread)
Dan Fitch
+13  A: 

:set paste is the way to go, but if you forget, as I often do, then if you are using a language with {} as the open/close of blocks, then doing a =% on the first { or last } will reapply the indenting.

Craig H
Nice trick with the =%. I forgot = works on ranges.
strager
This actually works with any language plugin that correctly defines blocks -- ruby, for example, can do this from the `end`.
Dan Fitch
+1  A: 

add this to your .vimrc and use it with the F2 key to toggle paste status before and after you add in chunks of code:

set pastetoggle=<F2>
kguest
You forgot to enclose code in ```.
ZyX
That's fixed now ;)
kguest