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):
- :set filetype=html
- :filetype indent on
- :e
- 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:
- :set tabstop=4
- :set shiftwidth=4
- :set expandtab
- :retab
That’s all!