Sep 24 2008

Vim: paste indent problems

Tag: , , , , , , mr-euro @ 14:17

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.


If you found this post interesting I recommend the following reading:


7 Responses to “Vim: paste indent problems”

  1. Srikanth says:

    Thanks, it helped me today :)

  2. mr-euro says:

    @Srikanth

    Excellent!

  3. sid says:

    thanks for this.

  4. mr-euro says:

    @sid

    You are welcome!

  5. Tomtom says:

    Thank you very much for this hint !

  6. mr-euro says:

    @Tomtom

    My pleasure!

  7. simon says:

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

Leave a Reply