Vim messing up indentation on your pasted code?
I’ve had this issue forever. I want to paste in some code from somewhere else into a Vim session on some random box, but I have to remember to do “:set nocindent noautoindent nosmartindent noreallypleasedontindent” all the time. Well, I finally had some time to google for an actual solution to the issue (I knew I couldn’t be the only person in the world with this issue) and I found one that is sooooo easy. Ready? Next time you wanna paste in some code, run this first
:set paste
That’s it. Now paste in your code, and after the angels stop singing in your brain, you can get back your normal indentation settings with:
:set nopaste
As you might expect.

Vim messes up the indentation only when pasting in insert mode. You can do “controlled paste” using three keys “+P (double-quote, plus sign, capital ‘P’).
tnx… was bugging me also for some times now…
I’ve seen configs that map that sequence toggle to something like F5. So, you want to paste – hit F5 – paste then hit F5 again.
Another handy trick is that appending a bang to boolean options toggles them. So you can start with `:set paste!`, paste in your code, and then use your history to turn it off again.
You’re my hero!
(And not just for this, I like everything I’ve read on this blog so far. Although most of the time I just keep my virtual mouth shut… Thanks for everything you’ve written here!)
jetsaredim is right, it’s much easier to place
set pastetoggle=
in your ~/.vimrc and then when pasting in insert mode hit , middle button, .
The “+p thing is nice if your vim has access to the system clipboard, but sometimes it doesn’t (think a remote SSH session with no X11 forwarding).
Apologies for the double post, but WordPress gives me no “Preview” button and isn’t smart enough to escape </>
jetsaredim is right, it’s much easier to place
set pastetoggle=<f11>
in your ~/.vimrc and then when pasting in insert mode hit <f11>, middle button, <f11>.
The “+p thing is nice if your vim has access to the system clipboard, but sometimes it doesn’t (think a remote SSH session with no X11 forwarding).
Thanks for the tip!
This has driven me nuts and it never occurred to me that it was vim autoindenting. I assumed it was something screwy with putty’s terminal type handling!
Thanks again.
[...] Vim messing up indentation on your pasted code? share: [...]