I am trying开发者_JAVA百科 to adjust the width of tabs from Window -> Preferences -> General -> Editors -> Text Editors where there is a field for "Display Tab Width". But no matter what I enter there the size of indentations when I start a newline in a Java file is 4. Any ideas?
Java files have a special place to edit this setting. You need to go to
Window -> Preferences -> Java -> Code Style -> Formatter
And edit the profile.
Changing Window -> Preferences -> General -> Editors -> Text Editors tab preference is relevant for text files, not for java files as indicated here. You may create a text file in Eclipse and check that your tab size is really changed.
Check your Java->Code Style->Formatter settings, to see whether you're using tabs or spaces; you might be using spaces!
This setting is superseded for Java by the setting from the Code Style Formatter.
It can be found in Java / Code Style / Formatter (click Edit) / Indentation tab / Indentation
How to set tab size (number of spaces when you press Tab) in Eclipse or Eclipse CDT:
There are multiple places to check and things to do:
- Global Preferences:
- Window --> Preferences --> General --> Editors --> Text Editors --> change "Displayed tab width" from 4 to 2
- Window --> Preferences --> Java OR C/C++ [depending on which you're using] --> Code Style --> Formatter --> click "Edit" button for your "Active profile" --> click "Indentation" tab on top-left --> change "Indentation size" to 2, "Tab size" to 2, and "Tab policy" to "Spaces only".
- Project Preferences (these override the global settings for a given project):
- Project --> Properties (or just right-click on project in the folder list at the left, and go to "Properties") --> Java OR "C/C++ General" [depending on what you're using] --> Formatter --> either uncheck the box for "Enable project specific settings", OR check that box and ensure your settings are correct (refer to 1.2 above, beginning with the
click "Edit" button
step, but do it from this menu this time). - To test your new settings: return to your editor and press Enter with the cursor sitting immediately after an opening curly brace (
{
) somewhere. It should go to a new line with the proper indentation you've set above. - NB: watch out! If you press Enter on or after a line with different indentation from what you've set above, it will duplicate that wrong indentation (by copying the style of the lines just above) rather than accepting your new indentation settings. Do NOT think it isn't working in this case! It IS working! Just delete all of the old indentation and press Enter right after a
{
and you'll see everything works just fine.
Version tested: All tested in Eclipse IDE for C/C++ Developers, Version: Oxygen.3a Release (4.7.3a).
Duplicate or Related Questions & Answers:
- Google search for "Eclipse change tab size" - https://www.google.com/search?q=eclipse+change+tab+size&oq=eclipse+change+&aqs=chrome.0.69i59l3j0l3.2483j0j4&sourceid=chrome&ie=UTF-8
- https://superuser.com/questions/462221/how-do-i-reliably-change-the-indentation-width-in-eclipse
- Eclipse tab width not changing
Also Related
- Set line width vertical bar marker / print margin - Is there an Eclipse line-width marker?
After digging into this more deeply, I think I've figured this out:
Java file tab DISPLAY width
Confusingly, this is under "Formatter" in preferences (like the accepted answer says). But this is inconsistent location-wise with where the option for tab display width for the non-java file Editor is located.
/instance/org.eclipse.jdt.core/org.eclipse.jdt.core.formatter.indentation.size=16
Text file tab DISPLAY width
/instance/org.eclipse.ui.editors/tabWidth=2
I emphasize DISPLAY because this is just for rendering in Eclipse. It does not change the contents of your file, and has no effect on "Source -> Format".
精彩评论