Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Eclipse to insert tabs instead of spaces for Java content assist?

Is there any way to persuade Eclipse to use tabs instead of spaces for indenting its built-in Java content assist proposals (such as when creating an empty method which overrides a method in the parent class)?

The only setting I could find that looked relevant is in Windows/Preferences/General/Editors/Text Editors/Insert spaces for tabs, and this setting is unchecked.

(EDIT: Several posters have pointed out Java / Code Style / Formatter / Indentation / Tab policy, which is set to "Tabs only")

At the moment I have to remember to select them and re-format with <Ctrl>+<Shift>+<F>.

like image 362
Simon Nickerson Avatar asked Apr 14 '10 10:04

Simon Nickerson


People also ask

How do I get tabs in eclipse?

Ctrl + H > File Search tab.

Does Eclipse use tabs or spaces?

Eclipse - Tab to spaces And eclipse uses tabs for spaces by default. Tabs are always dependent on the environment and use 4 spaces in windows. If you use source code written with tab spaces, code is consistent across different environments.


1 Answers

I found the solution, thanks to Simon Nickerson's comment above, which is what the original poster was referring to in his edit. After having found the setting, I see what they are talking about. However, the way to it was given a few steps from the beginning of the path, so it fails to give a clear answer to people who are new to Eclipse or have been away from it for a few years, like me. so I'll give the full path here:

  1. Go to Project->Properties.
  2. In the resulting box, go to "Java Code Style"->Formatter.
  3. Click the "Configure Workspace Settings" link in the upper right of the box.
  4. Click the Edit button next to the name.
  5. Change "Tab policy:" drop down box to "Spaces only" because tabs are evil and inevitably result in poorly formatted code. >:-( This is also where you can actually affect the indentation size and tab size in your files.
  6. You will need to change the "Profile name" at the top of the box to save your changes.
  7. Thankfully, once you hit "OK", Eclipse will use your profile for your current project, so you're done after this step.

Now, what's really broken is the fact that these settings override the more up-front interface for changing similar-sounding settings in "Windows/Preferences/General/Editors/Text Editors/Insert spaces for tabs". To someone relatively new to the editor, this would seem like the place to set the default tabs = spaces setting. After finding the real answer, it's clear that this is just for general text editing, and language-specific settings need to be set up in the profiles for the languages at hand. This should be made a bit clearer for noobs, though. ;-)

like image 141
Jacob Avatar answered Sep 23 '22 06:09

Jacob