Indentation with spaces and the tab key in Eclipse. I've pulled quite some hair on this one and now that I found the solution, I decided to write it down, so I don't have to loose my precious hair anymore.

Everybody has its own preferences, but I like 2 space indentation for PHP (code style of Drupal) and 4 space indentation in Python. And when I press the tab key, I want to increase the indentation level, not insert a tab character. Doesn't seem like much to ask, but it took a long and frustrating process to get this working in Eclipse (3.5 aka Galileo at the time of this writing), Aptana Studio actually, with PDT for PHP development and the excellent Pydev for Python development.

There are lot of tab/indentation related options scattered around in the preferences and apparently you have to be careful about how you set them up.

  • First important thing: do not enable the "insert spaces for tabs" option under Preferences/General/Editors/"Text Editors". Enabling this one interferes rather aggressively with the other tab/indentation settings.

  • For PHP, I set the indentation policy to two spaces under Preferences/PHP/"Code Style"/Formatter and enabled "Tab key indents the current line" under Preferences/PHP/Editor/Typing

  • For Python, the settings are on one page under Preferences/Pydev/Editor: set the tab length and enable "Replace tabs with spaces when typing".

Now back to coding (as long as the tools don't get in the way again).