When pasting into Vim from an external source, for example when using ⌘-V in a terminal on macOS, you’ll find that pasted text loses its indentation.

That’s because Vim has no way to distinguish that the text you’re entering was pasted and not just typed in. Therefore, normal autoindenting and other formatting will be applied, destroying the existing indentation. Except, Vim does indeed have a way to distinguish pasted text!

:set paste

Simply turn on the paste option with :set paste and any text pasted from your system clipboard in insert mode will be entered as-is without additional indentation rules applied. You can revert to the original with nopaste

:set nopaste