If you use the paste command from your terminal emulator program, Vim will interpret the stream of characters as if they were typed. That will cause all kind of undesirable effects, particularly bad indendation.
To fix that, from command mode:
:set paste
Then move on to insert mode, with i, for...