The reason has been explained very well by the other posters.
Here I would like to provide a solution which handles the situation automatically.
With the following in your ~\.vimrc
let &t_SI .= "\<Esc>[?2004h"
let &t_EI .= "\<Esc>[?2004l"
inoremap <special> <expr> <Esc>[200~ XTermPasteBegin()
function! XTermPasteBegin()
set pastetoggle=<Esc>[201~
set paste
return ""
endfunction
you can paste freely without worrying about the auto-indentions.
If you work in tmux, then you have to write instead the following
function! WrapForTmux(s)
if !exists('$TMUX')
return a:s
endif
let tmux_start = "\<Esc>Ptmux;"
let tmux_end = "\<Esc>\\"
return tmux_start . substitute(a:s, "\<Esc>", "\<Esc>\<Esc>", 'g') . tmux_end
endfunction
let &t_SI .= WrapForTmux("\<Esc>[?2004h")
let &t_EI .= WrapForTmux("\<Esc>[?2004l")
function! XTermPasteBegin()
set pastetoggle=<Esc>[201~
set paste
return ""
endfunction
inoremap <special> <expr> <Esc>[200~ XTermPasteBegin()
The source is Coderwall if you would like to read more.
If you work in screen
the equivalent wrap-function is:
function! WrapForScreen(s)
if exists('$TMUX') || match($TERM, "screen")==-1
return a:s
endif
let screen_start = "\<Esc>P"
let screen_end = "\<Esc>\\"
return screen_start . a:s . screen_end
endfunction
Found in stapelberg's .vimrc
.