13

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?

dreeves
  • 26,430
  • 45
  • 154
  • 229

4 Answers4

21

: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
  • 7,949
  • 16
  • 49
  • 61
  • This actually works with any language plugin that correctly defines blocks -- ruby, for example, can do this from the `end`. – Dan Fitch Jan 23 '09 at 02:04
15

:set paste

J.J.
  • 4,856
  • 1
  • 24
  • 29
  • 1
    :set nopaste - to turn indenting back on – J.J. Jan 22 '09 at 19:53
  • 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 Jan 22 '09 at 19:57
  • 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. Jan 22 '09 at 20:00
  • 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 Jan 22 '09 at 21:02
  • @dreeves: automatically continued commenting is also disabled by :set paste – Dan Fitch Jan 23 '09 at 02:02
  • 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 Jan 23 '09 at 02:14
  • @dreeves: :set fo-=r :he fo-table for more info on all the stuff you can set – Dan Fitch Jan 23 '09 at 02:21
  • (doh, forgot no linebreaks in comments -- :he is a separate command obviously... and now I will stop derailing this comment thread) – Dan Fitch Jan 23 '09 at 02:22
  • This is also helpful: `au InsertLeave * set nopaste` -- no one wants that left on! – kenny Aug 16 '12 at 16:15
7

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
  • 3,804
  • 3
  • 29
  • 31
1

Another way to do this, assuming you have your system clipboard set up properly is to do

"*p

This will paste from the system clipboard.

Check your vim --version. On OS X you'll need +clipboard and on Linux +xterm_clipboard, I believe.

If you're on OS X, you can always brew install macvim and use mvim -v instead of the bundled Vim (it was not compiled with +clipboard).

ib.
  • 27,830
  • 11
  • 80
  • 100
Aaron Jensen
  • 6,030
  • 1
  • 30
  • 40
  • check your vim --version. look for +xterm_clipboard or +clipboard. I use mvim -v. I have -xterm_clipboard but +clipboard and it works fine. – Aaron Jensen Aug 17 '12 at 16:53