Vim copy/paste messing up indentaton

Whenever I copy something from another application and then go to paste it into vim, it messes up the indentation.

For example, just now I tried to copy the manifest.json file from the hello-world tutorial for creating chrome extensions.

It looks like this:

{
  "manifest_version": 2,

  "name": "One-click Kittens",
  "description": "This extension demonstrates a browser action with kittens.",
  "version": "1.0",

  "permissions": [
    "https://secure.flickr.com/"
  ],
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  }
}

but, when I paste it into vim, it looks like this: enter image description here

My vimrc is as follows:

"se t_Co=256
syntax enable

set nowrap
set mouse=a
set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab 
set number
set showcmd
set cursorline
set showmatch

execute pathogen#infect()
"filetype plugin indent on

"folding settings
set foldmethod=indent   "fold based on indent
set foldnestmax=10      "deepest fold is 10 levels
set nofoldenable        "dont fold by default
set foldlevel=1            

set clipboard=unnamed  "share one clipboard for everyhting    

It has something to do with this line:

execute pathogen#infect() "filetype plugin indent on

If I comment it out, The problem is resolved. However, this is what I use to achieve auto-indent for when I am coding in python. Is there another way to get auto-indent?


Solution 1:

In the terminal, Vim cannot distinguish between typed text (where you want automatic indenting), and pasted text. So there's the 'paste' option (and 'pastetoggle' to simplify handling), which when set disables auto-formatting and -indenting. An alternative is using graphical GVIM, which can detect that.

Or, you use Vim's clipboard access (if configured and working, which you need to try out), and use the "* / "+ registers for the selection / system clipboard, e.g. via "+p or :put +. Maybe pasting with the middle mouse button also just works; try it out!

Solution 2:

:set paste

or see http://vim.wikia.com/wiki/Toggle_auto-indenting_for_code_paste

(herearesomecharsbecausesuperuser.comthinksshortanswersarenotanygoodanswers)