Vim: paste indent problems

24 Sep
2008

On one of my boxes I came across this weird behaviour when pasting code into Vim at the terminal. All indentation was broken in a stepped way. Every line indented more and more creating a real mess to work with. This only happened when actually pasting content into the terminal. Typing worked as normal.

I initially thought that it had something to do with Putty‘s way of interacting with this specific box e.g. some configuration that was missing. Issues exist with carriage return (CR) and line feed (LF) handling across platforms. Nevertheless this was a dead end.

Digging around Vim’s extensive wiki provided some clues as to what was going on. Having ‘autoindent‘ or ‘smartindent‘ on creates havoc when pasting into Vim and therefore should be disabled. The problem is that these are nice features to have on when simply typing.

Vim has an alternative mode called ‘paste‘ that does not modify the pasted input in any way and therefore code looks just as in the original source. To enable ‘paste‘ mode during insert simply type ‘:set paste‘ and you are ready to paste. When finished pasting you probably want to leave ‘paste‘ mode by typing ‘:set nopaste‘.

As I am too lazy to type this every time I need to paste something I have now added the ‘pastetoggle‘ key into my profile’s .vimrc file as follows: ‘set pastetoggle=<F12>

From now on simply hit F12 on the keyboard and you will be in ‘paste‘ mode. Press F12 again to leave it.

18 Responses to Vim: paste indent problems

Avatar

Srikanth

September 26th, 2008 at 15:56

Thanks, it helped me today 🙂

Avatar

mr-euro

September 28th, 2008 at 13:03

@Srikanth

Excellent!

Avatar

sid

October 23rd, 2008 at 15:11

thanks for this.

Avatar

mr-euro

October 24th, 2008 at 11:23

@sid

You are welcome!

Avatar

Tomtom

November 13th, 2008 at 11:35

Thank you very much for this hint !

Avatar

mr-euro

December 18th, 2008 at 01:13

@Tomtom

My pleasure!

Avatar

simon

March 4th, 2009 at 09:53

While not the same thing, it’s note worthy: :set paste! toggles paste mode too.

Avatar

Fedyashev Nikita

November 19th, 2009 at 08:47

Thanks, dude! It was pretty annoying before 🙂

Avatar

Rob Bean

December 2nd, 2009 at 22:36

Thanks a bunch for posting this. Helped me from losing all my hair today.

Avatar

Sam

August 19th, 2010 at 15:08

Thanks for the tip. I’ve always used “set noai” in the paste for this, but it didn’t work when using a new terminal today, but “set paste” did.

Avatar

Rakesh

September 23rd, 2010 at 08:57

Thanks. Helped me.

Avatar

HowTo: Cómo pegar en Vim sin hacer un desmadre de indentación : Renich's Blog

February 3rd, 2011 at 23:34

[…] referencia http://mr-euro.com/vim-paste-indent-problems/ Comparte y […]

Avatar

Eduardo

May 11th, 2011 at 15:31

Saved+my+day!+I+have+to+work+remotely+on+some+texts+and+vim+kept+breaking+lines,+it+was+driving+me+insane!+Thanks+a+lot.

Avatar

Jeff

July 21st, 2011 at 21:37

This was extremely helpful thank you!!

Avatar

emil

September 14th, 2011 at 00:00

thanks much! that was helpful!

Avatar

awol

April 26th, 2013 at 22:42

one of those niggling problems i’ve had for aaaages. it finally got to me and I snapped and undertook a vengeful google search and found your site. thanks for the tip.

Avatar

Eli

June 4th, 2014 at 15:43

Thanks for the tip!

Avatar

Francesco

September 28th, 2014 at 07:46

Saved my day, thanks!

Comment Form

top