Changing Tabs to Spaces in Eclipse

Let’s say you are developing a high performance code using Eclipse on Windows, than you deploy it to Unix Server, which FTPs some of the code to IBM’s z/OS… Let’s just say you do :)

It is obvious that these different operating systems use different editors and might even use (z/OS – EBCDIC) different character encodings… That is where incorrect formatting/indentation can decrease readability and maintainability tremendously…

Here is one quick tip on how to ensure that indentation stays the same across all the systems/editors:

use spaces instead of tabs

Here is how to change the default Eclipse “tabbing behavior” to the unified “spacing” one. Some people refer to it as changing “Hard TABs to soft TABs”:

1. Open the default workspace in Eclipse (open Eclispse :) ) and go to “Window -> Preferences -> Java -> Code Style -> Formatter”

p

2. Click on “Show” button (if you previously edited this profile the button caption will be “Edit”)

show/edit profile window

and change the “Tab Policy” from “Tabs only” to “Spaces only”

3. Click “Ok”, choose the name for this profile and save it

save profile window

 

You are all set and ready for “indentational independence” ;)

 

12 comments

  1. Sweet – I was off and running in no time!

  2. Krzysztof Cygan

    thanks for the tip, tabs for indentation is some mistake, hopefully can be fixed

  3. When you select a block of lines and hit tab to indent the block, Eclipse ignores the ‘spaces only’ setting and inserts tab characters. This is a bug.

  4. No, don’t change to spaces just because some editors are broken.

  5. Works perfect! Thanks!

  6. GameRendering: Instead be dependent on everybody on your project using the same editor with same tab indentation?
    No thank you, I’ll much rather have source code that is editor independent and works right anywhere.

  7. When doing this, how do you get backspace to delete multiple spaces at once?

  8. thank you! Super helpful.

  9. thank you it worked…… :)

  10. thanks a lot this helped me to remove check style errors

  11. Thanks this is exactly what I was looking for. Is there a way just to convert spaces to tabs?

tell me something...
  1. (required)
  2. (valid email - optional)
  3. Captcha
  4. (required)