December 06, 2011

Disabling auto indentation for code pasting in vim

If you are a vim user and have automatic indentation enabled, then you have sure been through the trouble of trying to paste indented code and messing up the indentation completely. There is however a fix for this , you can temporarily disable code indentation when you are pasting text. Add the following to your vim configuration file (mostly ~/.vimrc) :

set pastetoggle=<key sequence>

You can set whatever key sequence you want and if you do not have any other mapping for that key sequence, you just need to hit the key sequence and then "i" for insert and paste the text. Insert will generally show you the mode, for example if you have paste mode enabled, you will see

-- INSERT (paste) --

You can disable the paste mode using the same sequence as above. On my machine I have the sequence  set to <leader><C-p>, (my leader key is bound to the default ",")

set pastetoggle=<leader><C-p>

So hitting, "," and then Ctrl-p toggles the mode on vim on my machine.