Wednesday, June 8, 2011

Replacing tabs with spaces in eclipse

Tabs are evil! They are interpreted differently by different editors. So, it is always better to setup your editor to replace tabs with a constant number of spaces. IMO, 4 spaces will be visually appealing. While working with eclipse, here's how to set this property: (assuming eclipse version "Helios Service Release 2")
Window -> Preferences -> Java -> Code Style -> Formatter -> New
Give your profile a name, something like 'Eclipse [only-spaces]' and inherit all the basic settings from the 'Eclipse [built-in]' profile. (You can choose to inherit from whichever other profile of your interest or you can directly edit one of your favorite profiles. For me, the built-in profile was sufficient). Also make sure that 'Open the edit dialog now' is checked in and then press 'Ok'.
In the dialog box which opens, select Indentation -> Tab policy -> Spaces only.
Set the 'Indentation size' and 'Tab size' to 4. (meaning 4 spaces, as I mentioned in the beginning).
Press Apply to save all your settings and from here onwards, eclipse will put spaces instead of tabs for the indentation!

The reason I created a new profile is because one can keep re-using this in all your future projects and also has the ability to export your profiles when you want to use this profile on some other machine!

No comments:

Post a Comment