Pasting code from another application to Vim is a nightmare. The autoindent features of Vim will drive you crazy. What help you out is the “paste mode”.
To paste code as is:
Past code to vim
But typing so many characters is not convenient enough. We can turn auto indenting for pastes on and off with pastetoggle:
The to paste code (in insert mode, of course):
Press F2 (toggles the ‘paste’ option on).
Press F2 (toggles the ‘paste’ option off).