913

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Brian Deacon
  • 19,494
  • 12
  • 36
  • 40
  • 1
    Check [the following link](http://rakesh.sankar-b.com/2011/08/22/convert-tabs-to-spaces-in-eclipse-and-file-in-linux/) that I had wrote which contains the steps to convert tabs to spaces in Eclipse and also in Linux VI editor. – Rakesh Sankar Jul 10 '12 at 12:23
  • related: [How can I get Eclipse to insert tabs instead of spaces?](http://stackoverflow.com/questions/2636589/how-can-i-get-eclipse-to-insert-tabs-instead-of-spaces-for-java-content-assist). – David Cary Nov 14 '12 at 23:19
  • 45
    @i3ensays because tabs are presented differently in different editors or environments whereas spaces are always consistent. – mmmdreg Apr 01 '14 at 10:38
  • 32
    @mmmdreg exactly my point. spaces are rigid and wasteful. Using tabs gives freedom to the viewer to configure their editor as desired. I like my tabs to display as 2 spaces, my colleague 4. This is not possible without use of tabs. My colleague here, and I, battle spaces when we each format in our respective editor; tabs are not an issue; this is why they were invented (I suspect). – i3ensays Apr 01 '14 at 18:26
  • 18
    @i3ensays amen, brother! I've been trying to make people understand this concept for over 10 years (http://www.rizzoweb.com/java/tabs-vs-spaces.html). What really alarms me is that, as programmers, you'd think everyone would understand the concept of abstractions (a tab is an abstraction of code indentation, while a fixed number of spaces is a hard-coded implementation) - but alas, it seems that many programmers can't see the abstraction here. – E-Riz May 09 '14 at 18:07
  • 32
    @i3ensays Your team should have a coding standard that clearly states how code is formatted. This would save time by preventing the battles you mention. You should adopt one asap but that standard will 100% certainly state that tabs are never to be used because this holy war was won by the "space people" ages ago so a standard won't help you. Aside from alignment issues one of the biggest reasons for spaces only is that revision control tools and code reviewers won't have to deal with this issue. I started on your side of this battle 20 years ago but eventually learned to embrace the space. – Night Owl Jun 22 '14 at 21:11
  • 15
    The reason is that people will inevitably end up with combinations of tabs and spaces and in some editors the code will not display correctly. If everyone just uses spaces then this is never a problem. – Eamonn McEvoy Dec 02 '14 at 11:05
  • 10
    Hi everybody! I'm the original Asker of The Question. Could someone with enough Hit Points close down the comments on this question? The actual question has been very thoroughly answered, and now it has devolved into re-litigating the tabs vs. spaces war. Tucker Carlson pretty much laid that argument to rest a few years ago on Crossfire. The correct answer is: tax cuts. – Brian Deacon Dec 08 '14 at 17:03
  • 3
    Amazing to see the last holdouts for "tab faction". Yes, spaces won. Get on with the program. Savings in bytes have not been the issue since 80s, and none of the other benefits really matter enough to offset problems caused by having to treat tabs as Very Special Little Characters. – StaxMan Apr 10 '15 at 20:46
  • 4
    @StaxMan if spaces had obtained a decisive win, then I would not now be looking up this answer and figuring out how to reconfigure eclipse to use spaces over tabs - to comply with my team's policy. In a decisive space win, eclipse would just use spaces by default. IOW the battle rages on. – emory Jan 21 '16 at 16:40
  • 1
    @emory alas. Right you are. Eclipse defaults are one of my pet peeves. Then again, IdeaJ also has its own idiosyncratic defaults, like "magically expanding lines". – StaxMan Jan 21 '16 at 23:39
  • For JavaScript specific case, see also https://stackoverflow.com/q/8993295/912046 which redirected me to here. – el-teedee Jan 21 '19 at 18:35
  • @emory this question and your team's policy show that spaces won. Probably, only people workin on Eclipse dev team use tabs. Maybe even they don't. – kap Jan 29 '19 at 14:34
  • I asked this question in 2011, before my elementary school child was born. Before java introduced try-with-resources. Every few months I get an email saying that somebody commented on it. Does beating a dead horse require more or less SO reputation than closing the question? – Brian Deacon Jan 30 '19 at 22:28
  • The problem has to do with the use of the backspace key. If I have a line that goes position->count->comment then I expect that if I put the text in as beginning->4->once upon a time – flounder Mar 11 '20 at 00:29

21 Answers21

1257

Java Editor

  1. Click Window » Preferences
  2. Expand Java » Code Style
  3. Click Formatter
  4. Click the Edit button
  5. Click the Indentation tab
  6. Under General Settings, set Tab policy to: Spaces only
  7. Click OK ad nauseam to apply the changes.

[Note: If necessary save profile with a new name as the default profile cannot be overwritten.]

Default Text Editor

Before version 3.6:

Window->Preferences->Editors->Text Editors->Insert spaces for tabs

Version 3.6 and later:

  1. Click Window » Preferences
  2. Expand General » Editors
  3. Click Text Editors
  4. Check Insert spaces for tabs
  5. Click OK ad nauseam to apply the changes.

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.

C / C++

  1. Click Window » Preferences
  2. Expand C/C++ » Code Style
  3. Click Formatter
  4. Click the New button to create a new profile, then OK to continue
  5. Click the Indentation tab
  6. Under General Settings, set Tab policy to: Spaces only
  7. Click OK ad nauseam to apply the changes.

HTML

  1. Click Window » Preferences
  2. Expand Web » HTML Files
  3. Click Editor
  4. Under Formatting, select the Indent using spaces radio button
  5. Click OK to apply the changes.

CSS

Follow the same instructions for HTML, but select CSS Files instead of HTML Files.

JSP

By default, JSP files follow the formatting preferences for HTML Files.

XML

XML files spacing is configured in Preferences.

  1. Click Window » Preferences
  2. Expand XML » XML Files
  3. Click Editor
  4. Select Indent using spaces
  5. You can specify the Indentation size if needed: number of spaces to indent.
Dave Ray
  • 38,258
  • 7
  • 79
  • 82
  • 19
    For those who have MyEclipse installed or any other IDE built on top of Eclipse, you may have another editor overriding your settings. This was the case for MyEclipse and JavaScript. I had my "Insert spaces for tabs" setting enabled in Text Editors, but tabs were still being used instead of spaces. I was about to call it a day and write it off as a bug, but then I saw a second editor for JavaScript (*.js ) files in the "Associated Editors" list. – John Feb 11 '10 at 17:45
  • 5
    This might be a helpful hint, but if you experience the same behavior, check if the file has any other associated editors because they may be overriding your settings. To do so, goto Window > Preferences > General > Editors > File Associations > then look for the file extension which is ruining your day in that list > click it and look at the Associated Editors list below – John Feb 11 '10 at 17:46
  • 1
    For JavaScript, I saw both Text Editor and MyEclipse JavaScript Editor which was flagged as the default. So one of two things can be done here, you can either set Text Editor to be your default or you can update the MyEclipse JavaScript Editor settings. I opted to do the latter and went to: Window > Preferences > MyEclipse > Files and Editors > JavaScript > Code Style > Formatter > Edit... > Tab policy: Hope this helps! – John Feb 11 '10 at 17:46
  • 2
    For XML I had no associated edotor but I had to go to Window > Preferences > XML > XML Files > Editor – Bill Comer Apr 21 '10 at 15:14
  • 107
    for existing Java file press Ctrl-A to highlight everything and then Ctrl-I to apply current indents. Otherwise existing tabs will prevent spaces in new lines created with Enter. It also replaces existing tabs with spaces. – topchef Apr 21 '11 at 01:41
  • 1
    @topchef That's exactly what was happening to me! Had all the settings as described but when I hit tab or Ctrl+I on a new line it gives the hard tabs. But as you said, the existing tabs were the culprit. Thanks :) – Surya Wijaya Madjid Sep 28 '11 at 09:38
  • In Eclipse 3.7.1, the first line should specify that "spaces only" is a dropdown menu option. – Richard Jan 24 '12 at 21:37
  • @topchef The option you are talking about is editor specific. e.g. its available for javascript editor of [Eclipse web tool platform](http://www.eclipse.org/webtools/) (in my case), It is not available to all file type. Specially when I have cfeclise for ColdFusion. – Vikas Mar 08 '13 at 06:07
  • 4
    For C/C++, one has to also change C/C++->"Code Style". This is by far the most annoying in this otherwise great software. – dashesy Sep 06 '13 at 01:11
  • I'm amazed how many people don't understand the value of tabs and insist on replacing them with spaces (see my comments to OP question). – i3ensays Apr 01 '14 at 18:29
  • 3
    To be more detailed, for C/C++ and Ecplipse 3.6 or later, the second step was: Window > Preferences > C/C++ > Code Style > Formatter > New, then choose any Profile name, ensure that `Open the edit dialog now` is enabled, hit `OK`, then under the `Indentation` tab, you'll see `Tab Policy`. Set it to `Spaces Only`. – Garrett May 04 '14 at 22:44
  • For the Mac, Preferences are under the "Eclipse" menu option. – Safa Alai Dec 07 '14 at 17:13
  • Any instructions for scala? I made the changes in the general editor and they do not seem to have gone into effect for my scala files – Glenn Strycker Jun 10 '15 at 19:38
  • C/C++: tabs in multi-line `#define` macros will not get replaced by Eclipse's auto-formatter, presumably because of the escaped newlines. Doing a regex find/replace of tabs with spaces is necessary. – CivFan Sep 17 '15 at 16:53
  • @John +1. I was baffled because I had configured seemingly everything to use spaces instead of tabs, but my JSP files were still formatting incorrectly. This answer and your comment pointed me towards the HTML specific editor... – scottysseus Oct 05 '15 at 14:11
  • When I follow these instructions, the "Ok" button gets grayed out, so I can't use them. Any ideas why? – Kelmikra Oct 15 '15 at 11:22
  • Setting it for general editor should set it for all editors, Eclipse settings are insane. – dashesy Jan 16 '16 at 21:40
  • Yet another place where these kind of settings could be overwritting the "spaces for tabs" behaviour is in the "Project Specific Settings". It happened to me in a legacy project where the project settings were commited to the repository. In my case, I had to change the settings in Project > Properties > Java Code Style > Formatter. It should be analogous to the other editors and settings described in this answer. – dcontard Feb 06 '18 at 16:21
  • This is how we got it to work in STS also based on eclipse 3.8 – Paul A. Trzyna Apr 05 '18 at 14:21
154

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
Alex Logvin
  • 426
  • 4
  • 8
Patrick Fisher
  • 7,445
  • 5
  • 30
  • 26
  • 1
    I see no Web or Javascript options in the root of Preferences (I do have a Web Perspective available, though) and I want to set tabs as spaces for just html, css, and javascript files. I'm on Helios Eclipse for Java Developers, any idea how I can get that option? – danny Aug 07 '11 at 20:20
  • These settings are what I see in Eclipse PDT (PHP Development Tools). Eclipse PDT is built on Eclipse WTP (Web Tools Platform), which includes JavaScript Development Tools (JSDT) and Source Editing of various web formats. This is probably what you're missing. – Patrick Fisher Sep 23 '11 at 21:13
  • +1 for the Java stuff which is absolutely necessary because its default settings override the general text editor settings. – Giulio Piancastelli Nov 10 '11 at 10:33
  • for C, Code Style -> Formatter -> Edit -> Indentation -> General Settings -> Tab Policy – andrew cooke Jul 25 '13 at 21:34
  • Not suit with **Eclipse PDT Luna 4.4** – Do Nhu Vy Nov 25 '14 at 02:28
  • Also be careful to update specific other editors that have their own formatter e.g. the Ant file editor vs the XML editor and so on. – Manfred Moser Dec 04 '14 at 18:02
  • 1
    Thanks! This works well in Mars. It's disappointing that the Text Editor options exist yet do nothing! – paiego Mar 19 '15 at 05:03
38

From changing tabs to spaces in eclipse:

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

Alex Logvin
  • 426
  • 4
  • 8
Brian Deacon
  • 19,494
  • 12
  • 36
  • 40
27

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)
Alex Logvin
  • 426
  • 4
  • 8
Antonio Leite
  • 271
  • 3
  • 2
24

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.

Alex Logvin
  • 426
  • 4
  • 8
Nick Chadwick
  • 280
  • 3
  • 7
19

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

Alex Logvin
  • 426
  • 4
  • 8
Sumit Singh
  • 23,530
  • 8
  • 71
  • 99
10

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.

user1978019
  • 2,170
  • 23
  • 37
  • For a more modular fix to this problem, the user can also do a Find/Replace for `\t` (make sure regular expressions are enabled) and replace with the desired amount of space characters – jlewkovich May 07 '14 at 21:18
  • The key for formatting file on Windows is quite similar - `Ctrl+Shift+F`. – A.Alessio Mar 18 '20 at 14:37
6

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"

Khokhar
  • 655
  • 1
  • 8
  • 21
6

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
Alex Logvin
  • 426
  • 4
  • 8
Michał Orliński
  • 1,062
  • 12
  • 13
  • 1
    On Ubuntu 14.04.03 LTS and eclipse 4.5 I cannot change the identation size is always locked and not changeable. This solved my problem. – TekTimmy Sep 01 '15 at 11:28
6

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.

Luke H
  • 67
  • 7
6

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

karel
  • 3,880
  • 31
  • 37
  • 42
Saikat
  • 8,190
  • 12
  • 69
  • 94
4

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

Alex Logvin
  • 426
  • 4
  • 8
Sandeep
  • 59
  • 4
4

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

Santosh
  • 49
  • 2
4

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"/>
Community
  • 1
  • 1
dillip pattnaik
  • 1,592
  • 15
  • 14
3

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

Chester
  • 39
  • 1
1

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

Christophe Roussy
  • 13,732
  • 2
  • 75
  • 75
1

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,

Quoc Truong
  • 738
  • 5
  • 11
0

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!

piet.t
  • 11,035
  • 20
  • 40
  • 49
paiego
  • 3,270
  • 28
  • 37
0

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)
Alex Logvin
  • 426
  • 4
  • 8
shonky linux user
  • 5,223
  • 2
  • 36
  • 66
0
  • 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.
Wai Ha Lee
  • 7,664
  • 52
  • 54
  • 80
Sam
  • 5
  • 4
0

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

Ironcache
  • 1,679
  • 22
  • 31