By default Eclipse indents with a hard tab character. How do I change it to spaces?
By default Eclipse indents with a hard tab character. How do I change it to spaces?
Spaces only[Note: If necessary save profile with a new name as the default profile cannot be overwritten.]
Before version 3.6:
Window->Preferences->Editors->Text Editors->Insert spaces for tabs
Version 3.6 and later:
Note that the default text editor is used as the basis for many non-Java editors in Eclipse. It's astonishing that this setting wasn't available until 3.3.
Spaces onlyFollow the same instructions for HTML, but select CSS Files instead of HTML Files.
By default, JSP files follow the formatting preferences for HTML Files.
XML files spacing is configured in Preferences.
From changing tabs to spaces in eclipse:
Window » Preferences » Java » Code Style » Formatter » Edit » Indentation (choose "Spaces Only")
Go to Window -> Preferences and type tab as the search text (which will display a dialog like below):
Go to the highlighted sections to change the tab policy. For the Editor settings it is mostly Indent using spaces (also shown above).
For Formatter settings you need to edit the active profile and update Tab policy (available under `Indentation -> General settings:
You can definitely use XML file to configure your formatter of the coding style. The formatter file helps you to have the same coding style and guidelines across your team members.
Adding these below setting variables would have a tab= 2 spaces and convert to space even you probably use Tab shorthand key when coding using Eclipse.
<setting id="org.eclipse.jdt.core.formatter.tabulation.size" value="2"/>
<setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="space"/>
<setting id="org.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentations" value="yes"/>
Best,
As an augmentation to the other answers, on Mac OS X, the "Preferences" menu is under Eclipse, not Window (unlike Windows/Linux Eclipse distributions). Everything else is still the same as pointed out by other answers past this point.
IE: Java Formatter available under:
Eclipse > | # Not Window!
Preferences > |
Java > |
Code Style > |
Formatter |
From here, edit the formatter and the tab policy can be set under "Indentation".