Vim Set Paste

Sometimes you will paste something into your vim session and the indentation gets all messed up. The reason is due to automatic indentation. Normally the auto-indenting that vim does is very useful. Automatically indenting your line saves you the needs to contently tab or space bar to where you want the cursor to be.

The problem with pasting into vim is that the code you have copied most likely already has its own indentation. This preexisting indentation is added to vim's automatic indentation, leading to every line being more indented than the one before.

This all stems from the fact that when you copy and paste something into vim, vim treats it as if it were regular keyboard input.

To be able to paste without automatic indentation ruining the format of the code, you must set the 'set paste' command in the command-line mode. To do this, press escape to make sure you are not in insert mode. Then press the colon ':' key, type in 'set paste' and press enter.

Now auto-indentation is turned off and when you paste something into vim, the original code's indentation will be preserved.

To undo the 'set paste' command, do the same thing as above, only inputing 'set nopaste' as the command. This will bring vim back to auto indenting every time you start a new line.