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?

4 Answers 4

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.

1
  • This actually works with any language plugin that correctly defines blocks -- ruby, for example, can do this from the end.
    – Dan Fitch
    Jan 23, 2009 at 2:04
15

:set paste

9
  • 1
    :set nopaste - to turn indenting back on
    – J.J.
    Jan 22, 2009 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, 2009 at 19:57
  • You could use Key Mapping to bind it to a set of keys. But I have never tried this. shlomifish.org/lecture/Vim/beginners/slides/slide30.html
    – J.J.
    Jan 22, 2009 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, 2009 at 21:02
  • @dreeves: automatically continued commenting is also disabled by :set paste
    – Dan Fitch
    Jan 23, 2009 at 2:02
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>
0
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).

1
  • check your vim --version. look for +xterm_clipboard or +clipboard. I use mvim -v. I have -xterm_clipboard but +clipboard and it works fine. Aug 17, 2012 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.