HOWTO properly paste code into vi/vim

04 Sep 2013, 12:35

I’m sure you’ve done this before, you copy a big block of text or code and paste it into vi/vim (from now on referred to simply as vi). Instead of looking like you want it to, it takes every tab and just autoindents like crazy giving you a mess to clean up. So, for example, you copy the following

Yikes, that’s no good. So instead of going line by line and re-orienting the code (which yes, I used to do), you just need to paste correctly into vi. To do this, when you’re in the editing mode in vi, hit : and then type

set paste

Now back in the editing mode when you hit i for insert, you’ll see the notification – INSERT (paste) –. Now you can paste in and all of the formatting will look just like the original. When you’re done, if you want to turn this function off, hit : and type

set nopaste

While this is a small thing, I wish I had known about this, I don’t know, like TEN YEARS AGO! :)