October 28, 2009

Vim tips for the Windows user

If you are like me and want or are forced to use Windows on your desktop or laptop you most probably use putty to ssh into your servers. And also do a lot of copy / paste when editing files with vim. So you probably have noticed that when pasting in vim using right-click in putty (or any other way, doesn’t really matter) vim introduces a lot of white spaces, or transforms your tabs into white spaces or does crazy indenting in general. There are a few tips to work-around this.

1) enter command mode ( using : ) and type:

:set noautoindent

This will stop vim from messing your whole hard-worked indenting, but will not solve your white spaces problem, for this go to tip number 2. If you want to go back to writing code with vim’s help with indenting type:

:set autoindent

2) enter command mode ( using : ) and type:

:set paste

This enables vim’s “paste mode” and will allow you to paste lines that are already indented without any modifications (tabs to white-spaces) or re-indenting.

To exit this mode type:

:set nopaste

But what if you already have a file with lots of lines that was already messed with and saved to disk. Well there is a way to solve this in tip number three:

3) Go into Visual mode (by pressing the key v) and select the whole text (or just the lines you want fixed) using the arrow keys (left / right selects through a line, up / down selects or unselects whole lines) and when you are done just press the = key and vim will solve your indenting for you.

That’s it! Hope now your vim-ing experience will be a much better one!