[eclipse] How do I change Eclipse to use spaces instead of tabs?

By default Eclipse indents with a hard tab character. How do I change it to spaces?

This question is related to eclipse formatting tabs whitespace

The answer is


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

  • Ant » Editor » Formatter » Tab size: (set to 4)
  • Ant » Editor » Formatter » Use tab character instead of spaces (uncheck it)

Go to Window -> Preferences and type tab as the search text (which will display a dialog like below):

enter image description here

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:

enter image description here


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...


From changing tabs to spaces in eclipse:

Window » Preferences » Java » Code Style » Formatter » Edit » Indentation (choose "Spaces Only")


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".


From changing tabs to spaces in eclipse:

Window » Preferences » Java » Code Style » Formatter » Edit » Indentation (choose "Spaces Only")


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.


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):


  1. Found org.eclipse.wst.html.core.prefs file which should be in your_workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/
  2. Add/Change to line in file:
indentationChar=space

indentationSize=4

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 :-)


In eclipse format xml:

For tab:

<setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="tab"/>

For space:

<setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="space"/>

For Default Editor:

Window » Preferences » Editors » Text Editors » Insert spaces for tabs

enter image description here

For Java editor

Window » Preferences » Java » Code Style » Formatter » Edit » Indentation » Tab policy "Spaces Only"

enter image description here


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 :-)


In eclipse format xml:

For tab:

<setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="tab"/>

For space:

<setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="space"/>

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.


For CDT:

  • Go to Window/Preference » C/C++ » Code Style » Formatter » New
  • Create a new one because the built in profile can not be changed
  • MyProfile (choose one name for the profile)
  • Indentation » Tab Policy (choose Spaces only)

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".


For CDT:

  • Go to Window/Preference » C/C++ » Code Style » Formatter » New
  • Create a new one because the built in profile can not be changed
  • MyProfile (choose one name for the profile)
  • Indentation » Tab Policy (choose Spaces only)

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!


  • Click Window » Preferences
  • Expand Java » Code Style
  • Click Formatter
  • click new
  • Select the profile name
  • Click ok
  • Click the Edit button
  • Click the Indentation tab
  • Under General Settings, set Tab policy to: Spaces only
  • Click OK.

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,


From changing tabs to spaces in eclipse:

Window » Preferences » Java » Code Style » Formatter » Edit » Indentation (choose "Spaces Only")


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.


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"


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.


For Default Editor:

Window » Preferences » Editors » Text Editors » Insert spaces for tabs

enter image description here

For Java editor

Window » Preferences » Java » Code Style » Formatter » Edit » Indentation » Tab policy "Spaces Only"

enter image description here


Go to Window -> Preferences and type tab as the search text (which will display a dialog like below):

enter image description here

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:

enter image description here


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:

  • Right-Click current Project in Package Explorer
  • Properties » Java Code Style
  • Turn off all the project-specific options

This will generally only be an issue if you import someone else's code into your Eclipse.


  • Click Window » Preferences
  • Expand Java » Code Style
  • Click Formatter
  • click new
  • Select the profile name
  • Click ok
  • Click the Edit button
  • Click the Indentation tab
  • Under General Settings, set Tab policy to: Spaces only
  • Click OK.

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):


  1. Found org.eclipse.wst.html.core.prefs file which should be in your_workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/
  2. Add/Change to line in file:
indentationChar=space

indentationSize=4

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!


Window->Preferences->Java->Code Style->Formatter->Edit->Indentation = "Spaces Only"


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:

  • Right-Click current Project in Package Explorer
  • Properties » Java Code Style
  • Turn off all the project-specific options

This will generally only be an issue if you import someone else's code into your Eclipse.


From changing tabs to spaces in eclipse:

Window » Preferences » Java » Code Style » Formatter » Edit » Indentation (choose "Spaces Only")


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...


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


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"


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


Window->Preferences->Java->Code Style->Formatter->Edit->Indentation = "Spaces Only"


For the default text editor:

  • General » Editors » Text Editors » Insert spaces for tabs (check it)

For PHP:

  • PHP » Code Style » Formatter » Tab policy (choose "spaces")
  • PHP » Code Style » Formatter » Indentation size (set to 4)

For CSS:

  • Web » CSS » Editor » Indent using spaces (select it)
  • Web » CSS » Editor » Indentation size (set to 4)

For HTML:

  • Web » HTML » Editor » Indent using spaces (select it)
  • Web » HTML » Editor » Indentation size (set to 4)

For XML:

  • XML » XML Files » Editor » Indent using spaces (select it)
  • XML » XML Files » Editor » Indentation size (set to 4)

For Javascript:

  • Javascript » Preferences » Code Style » Formatter » Edit » Indentation (choose "spaces only")
  • Rename the formatter settings profile to save it

For Java:

  • Java » Preferences » Code Style » Formatter » Edit » Indentation (choose "spaces only")
  • Rename the formatter settings profile to save it

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

  • Ant » Editor » Formatter » Tab size: (set to 4)
  • Ant » Editor » Formatter » Use tab character instead of spaces (uncheck it)

For the default text editor:

  • General » Editors » Text Editors » Insert spaces for tabs (check it)

For PHP:

  • PHP » Code Style » Formatter » Tab policy (choose "spaces")
  • PHP » Code Style » Formatter » Indentation size (set to 4)

For CSS:

  • Web » CSS » Editor » Indent using spaces (select it)
  • Web » CSS » Editor » Indentation size (set to 4)

For HTML:

  • Web » HTML » Editor » Indent using spaces (select it)
  • Web » HTML » Editor » Indentation size (set to 4)

For XML:

  • XML » XML Files » Editor » Indent using spaces (select it)
  • XML » XML Files » Editor » Indentation size (set to 4)

For Javascript:

  • Javascript » Preferences » Code Style » Formatter » Edit » Indentation (choose "spaces only")
  • Rename the formatter settings profile to save it

For Java:

  • Java » Preferences » Code Style » Formatter » Edit » Indentation (choose "spaces only")
  • Rename the formatter settings profile to save it

Examples related to eclipse

How do I get the command-line for an Eclipse run configuration? My eclipse won't open, i download the bundle pack it keeps saying error log strange error in my Animation Drawable How to uninstall Eclipse? How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Class has been compiled by a more recent version of the Java Environment Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 "The POM for ... is missing, no dependency information available" even though it exists in Maven Repository The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat

Examples related to formatting

How to add empty spaces into MD markdown readme on GitHub? VBA: Convert Text to Number How to change indentation in Visual Studio Code? How do you change the formatting options in Visual Studio Code? (Excel) Conditional Formatting based on Adjacent Cell Value 80-characters / right margin line in Sublime Text 3 Format certain floating dataframe columns into percentage in pandas Format JavaScript date as yyyy-mm-dd AngularJS format JSON string output converting multiple columns from character to numeric format in r

Examples related to tabs

Visual Studio Code - Convert spaces to tabs Sublime Text 3, convert spaces to tabs Bootstrap 3: Keep selected tab on page refresh Change tab bar tint color on iOS 7 Stacked Tabs in Bootstrap 3 C++ printing spaces or tabs given a user input integer Open link in new tab or window How to make a new line or tab in <string> XML (eclipse/android)? Find Active Tab using jQuery and Twitter Bootstrap How to make the tab character 4 spaces instead of 8 spaces in nano?

Examples related to whitespace

How to create string with multiple spaces in JavaScript git: fatal: I don't handle protocol '??http' Show whitespace characters in Visual Studio Code What is the symbol for whitespace in C? How to print variables without spaces between values Trim whitespace from a String How do I escape spaces in path for scp copy in Linux? Avoid line break between html elements Remove "whitespace" between div element How to remove all white spaces in java