sensimevanidus

19 December, 2010

Fixing indentation and converting tabs to spaces in VIM

I use VIM as the default text editor on my Ubuntu box and generally work on HTML files. It’s a common problem of developers to edit a malformed (e.g. indentations are bad, tabs are used instead of spaces) source code.

When I encounter such a situtation, I enter the following commands in VIM to make the source code more readable (I wrote html as the filetype; but of course you should change it to your needs):

  1. :set filetype=html
  2. :filetype indent on
  3. :e
  4. gg=G

These 4 commands fixes the indentations of the file; but it’s not enough. I tend to convert tabs to spaces and fix the indention to 4 columns:

  1. :set tabstop=4
  2. :set shiftwidth=4
  3. :set expandtab
  4. :retab

That’s all!