Pasting in Vim

I find it very annoying when I paste text into a Vim, and it decides to re-tab everything and comment all lines after a comment. I decided to see if I could find a solution because “:set nosi noai” doesn’t do the trick. Stack Overflow has a solution that works very well:

<Esc> :set paste
<paste all you want>
<Esc> :set nopaste

It’s such a simple solution for a problem that has annoyed me for quite a while.