Vim Paste Turning Off Autoindent

Reference: Turning off auto-indent when pasting text into Vim

When pasting code into Vim, automatic indentation can sometimes shift the pasted lines farther and farther to the right. Vim has a special paste mode for this case.

Before pasting, run:

:set paste

Then paste your code. Vim should show -- INSERT (paste) -- while paste mode is active.

After pasting, turn paste mode off again so that normal auto-indentation works while you type:

:set nopaste

Typing those commands every time is cumbersome, so I map <F3> to toggle paste mode while editing. Add this to .vimrc:

set pastetoggle=<F3>

After that, press <F3> before pasting, paste the text, and press <F3> again to return to normal editing.

In newer Vim or terminal setups, bracketed paste support may handle this automatically. If pasting still breaks indentation, :set paste, :set nopaste, or pastetoggle remains the reliable fallback.

Leave a Reply