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. :-)

  • http://konryd.blogspot.com Konrad

    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’).

  • http://sharbas.blogspot.com garbas

    tnx… was bugging me also for some times now…

  • jetsaredim

    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.

  • http://www.sapphirepaw.org/ sapphirecat

    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.

  • Goofy

    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!)

  • http://gedmin.as Marius Gedminas

    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).

  • http://gedmin.as Marius Gedminas

    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).

  • Maarten H

    Thanks for the tip!

  • Jeff

    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.

  • Pingback: Setup Subversion (svn) | MarcJStuff

  • Chas. Owens

    Even better way is to integrate your system’s clipboard with vim’s. Vim has two registers “* (primary selection) and “+ (clipboard) for this purpose. See :help registers, :help clipboard, or http://vim.wikia.com/wiki/Accessing_the_system_clipboard for more info. “*p behaves the same way p does, but the source is the primary selection instead of the default vim register.

  • wardi

    or even faster — :a paste .

  • smeagol271006

    Also in insert mode
    ctrl +r + ctrl + o + +