Tag Archives: auto-indent

Auto-indent + Paste = Murder

I use Vim pretty much exclusively as my IDE for all programming tasks be it Python, HTML, JavaScript… whatever. One of the Nifty Features™ of vim is auto-indentation which saves a lot of extra tab keystrokes in the longrun. The side effect of this Nifty Feature™ is that when you go to paste code from another source into a file you’re editing, it goes berserk and indents everything you just pasted. If the code you’re pasting is already indented, you end up with nested double indentation, and with languages like Python where whitespace is significant this can be a huge pain in the ass.

So I finally had enough and decided to share this solution along. This is using Vim’s pastetoggle, which allows you to map a keystroke to toggle auto-indentation when you want to paste stuff. Put this into your .vimrc:

" map <F3> to toggle paste to prevent auto-indent mangling when pasting
" http://vim.wikia.com/wiki/VimTip906
noremap <F3> :set invpaste paste?<CR>
set pastetoggle=<F3>
set showmode

To use it, you hit F3 to toggle paste mode, paste what you need to paste, then hit F3 again to turn it back off. And done!