Reputation: 21972
By default Eclipse indents with a hard tab character. How do I change it to spaces?
Upvotes: 955
Views: 576744
Reputation: 20531
Using EditorConfig is a solution
Install editor config Eclipse plugin
Create an .editorconfig
with following content:
root = true
[*]
indent_style = space
indent_size = 4
insert_final_newline = true
charset = utf-8
You can do even more as changing the line ending style to LF
using end_of_line = lf
This also prepares usage of spaces in IntelliJ and VS.Code.
Upvotes: 2
Reputation: 1622
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,
Upvotes: 2
Reputation: 1842
<setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="tab"/>
<setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="space"/>
Upvotes: 5
Reputation: 16900
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:
Upvotes: 8
Reputation: 6428
And don't forget the ANT editor
For some reason Ant Editor does not show up in the search results for 'tab'
or 'spaces'
so can be missed.
Under Windows > Preferences
Tab size:
(set to 4)Use tab character instead of spaces
(uncheck it)Upvotes: 0
Reputation: 1408
I found the solution this problem very simple and which works always. It is change the eclipse setting file.
For example (change HTML indentation size):
org.eclipse.wst.html.core.prefs
file which should be in your_workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/
indentationChar=space
indentationSize=4
Upvotes: 5
Reputation: 3787
In eclipse mars (EE) on Mac OS X, the only way I could find this in the preferences was to open the Preference dialog and type Formatter, then select Java->Code Style->Formatter.
Java->Code Style has no access to Formatter!
Upvotes: 0
Reputation: 59
In Eclipse go to Window » Preferences then search for Formatter.
You will see various bold links,
click on each bold link
and set it to use spaces
instead of tabs
.
In the java formatter link, you have to edit the profile
and select the tab policy, spaces only
in indentation tab
Upvotes: 4
Reputation: 8063
For the default text editor:
Insert spaces for tabs
(check it)For PHP:
Tab policy
(choose "spaces")Indentation size
(set to 4)For CSS:
Indent using spaces
(select it)Indentation size
(set to 4)For HTML:
Indent using spaces
(select it)Indentation size
(set to 4)For XML:
Indent using spaces
(select it)Indentation size
(set to 4)For Javascript:
Indentation
(choose "spaces only")For Java:
Indentation
(choose "spaces only")Upvotes: 158
Reputation: 271
For CDT:
Tab Policy
(choose Spaces only)Upvotes: 27
Reputation: 15906
For Default Editor:
Window » Preferences » Editors » Text Editors » Insert spaces for tabs
For Java editor
Window » Preferences » Java » Code Style » Formatter » Edit » Indentation » Tab policy
"Spaces Only"
Upvotes: 23
Reputation: 21972
From changing tabs to spaces in eclipse:
Window » Preferences » Java » Code Style » Formatter » Edit » Indentation
(choose "Spaces Only")
Upvotes: 39
Reputation: 310
Just a quick tip for people stumbling across this thread; there is one more place where this setting can also be set, in your project!
Eclipse supports project-specific settings, and some projects will use their own, un-managed tabs/spaces settings, which won't show up anywhere except the current project Properties.
This can be managed through:
This will generally only be an issue if you import someone else's code into your Eclipse.
Upvotes: 25
Reputation: 1759
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".
Upvotes: 0
Reputation: 17049
Also consider using an .editorconfig file: https://marketplace.eclipse.org/content/editorconfig-eclipse. Someone not using Eclipse may also benefit from this, in the worst case it can serve as a guideline. NOTE: I will not enter the tabs vs space wars but use spaces FTW :-)
Upvotes: 2
Reputation: 40005
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 only
Follow 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.
Upvotes: 1313
Reputation: 79
Be sure to check the java formater since it overwrites the "insert spaces for tabs" setting. Go to:
Java->Code Style"->Formatter->Edit->Identation
Note: you will need to create a custom format to be able to save your configuration.
Upvotes: 7
Reputation: 5
Upvotes: 0
Reputation: 3336
When I faced this problem I had "use spaces for tabs" set to true everywhere I could find, and yet I was still getting tabs. It ended up being because tabs were used elsewhere in the file and it was trying to do smart indentation or something frustrating.
It was resolved by selecting the entire contents of the file and pressing, on a mac, command+shift+f
. This applies the given formatting to a file. I do not know the same keybinding on windows, but give that a try. At that point I begin getting the expected behavior.
Upvotes: 10
Reputation: 49
Window->Preferences->Java->Code Style->Formatter->Edit->Indentation = "Spaces Only"
Upvotes: 4
Reputation: 685
Eclipse IDE for C/C++ Developers, Version: Helios Service Release 2
You need to create new profile by pressing New button inside "Window->Preferences->Code Style"
Go to Indentation tab and select "Tab policy = Space only"
Eclipse IDE for C/C++ Developers, Version: Kepler Service Release 1
Follow the path below to create new profile: "Window > Preferences > C/C++ > Code Style > Formatter"
Go to Indentation tab and select "Tab policy = Space only"
Upvotes: 6
Reputation: 39
Don't miss Tab policy for both of * Spaces only * Use spaces to indent wrapped lines
I checked only the latter thing and left the Combobox as Tabs Only which kept failing CheckStyle.. FYI, I'm talking about Preferences > Java > Formatter > Edit...
Upvotes: 3