How to configure Eclipse to use spaces instead of tabs?

When writing a maintainable code, indentation plays important role. This may be one reason Python requires proper indentation as the programming syntax. A traditional tab indents 8 spaces, and we all know 8 spaces are just to much for coding indentation. We all opt to use 4 spaces for programming indentation, but how do you accomplish this? We can configure a TAB to indent only 4 whitespaces, or use 4 physical spaces? There are tradeoffs, and it's a matter of personal taste. I used to prefer TABS over SPACES, but if you're working in a team the lines may not line up correctly if developers mix and match TABS and SPACES. So, to make the indentation consistent, a coding guideline must be placed among multiple developers.

My recent programming practice is limited to mostly Java and PHP, and I use Eclipse as a preferred IDE. At times, I also login directly to Linux servers and use VIM to make quick edits to configuration files and scripts such as bash, perl and php files. Using 4 SPACES (4 bytes) over 1 TAB (1 byte) increases file size, and also slow down serving web pages. However, recent technology advancement in server technology and increase in Internet bandwidth minimize this effect, so why not just use 4 SPACES instead of 1 TAB and make the code readable and maintainable. The newer coding standards spells out to use 4 spaces over 1 tab, and I think we are moving into right direction. We spend a lot of time reading code than writing them, so mind as well write readable code by using 4 SPACES for indentation.

So, how do we configure Eclipse to insert 4 spaces whenever you hit a TAB key? Depending on the version of Eclipse you use, you may have use a slightly different steps. Here is the steps you'll follow on Ind

On Eclipse Menu:

Windows -> Preferences: -> General -> Editors -> Text Editors
Set "Displayed tab width" to 4, and check mark "Insert spaces for tabs".

If you're using PHP with Eclipse, you may also want to add PHP content type if you haven't installed PDT or PHP Eclipse plug-in.

Windows -> Preferences: -> General -> Content Types: -> Text -> PHP Content Type
Add *.inc and *.php file associations with the PHP Content Type

While at it, you may also want to change the Text Encoding to UTF-8 and newline character to LF (\n) ("Line Feed") on Linux instead of CR (\r) ("Carriage Return") followed by LF (\n) for MS-DOS.

Windows -> Preferences: -> General -> Workspace:
Set "Text file encoding" to "Other" and choose "UTF-8".
Also, set New text file line delimeter to "Other" and choose "Unix" for LF (\n).

Some coding standards such as Drupal, Rails and Google (C++ Style Guide) calls for only 2 spaces for indentation. Two spaces as indentation is gaining popularity in recent years, so who knows it may become a de facto standard in a few years. But until then, we may stick with 4 spaces.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.