How to configure Eclipse to use spaces instead of tabs?

When writing maintainable code, indentation plays an 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 too 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 log in 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 slows down serving web pages. However, recent technology advancements in server technology and increases 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 spell out to use of 4 spaces over 1 tab, and I think we are moving in the right direction. We spend a lot of time reading code than writing it, 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 to use slightly different steps. Here are the steps you'll follow on Ind

On Eclipse Menu:

Windows -> Preferences: -> General -> Editors -> Text Editors Set "Displayed tab width" to 4, and checkmark "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 the New text file line delimiter to "Other" and choose "Unix" for LF (\n).

Some coding standards such as Drupal, Rails, and Google (C++ Style Guide) call 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.

Share this post

Comments (0)

    No comment

Leave a comment

All comments are moderated. Spammy and bot submitted comments are deleted. Please submit the comments that are helpful to others, and we'll approve your comments. A comment that includes outbound link will only be approved if the content is relevant to the topic, and has some value to our readers.


Login To Post Comment