How to make Eclipse use spaces instead of tabs in Java editor

I use Eclipse IDE extensively to write Java programs for testing and example purpose, but when I copy those program in any text editors e.g. VIM, Notepad, TextPad or Edit plus, the indentation goes weird. I see a lot of white spaces which makes the program wider than expected. This happens because when you copy Java program from Eclipse to a text editor, tabs are converted to spaces and different editor has the different settings of tabs. UNIX text editors prefer tab is 8 spaces, Windows text editors, and IDEs  e.g. Eclipse treat tabs as 4 spaces. If you are like many Java programmer who is more comfortable with space than tabs because they give a true sense of spacing, you can always change the Java editor settings to use space instead of tabs in Eclipse. In this article, I am going to share how to make Eclipse uses spaces instead of tabs for Java editor, which you use while writing Java programs.


Tabs or Spaces

The tabs vs space war are is as old as God vs Demons but for me its more of a choice and consistency. If your project is using tabs then you should follow the same setting as nobody wants an SVN commit which says 100% file is changed only to find that tabs were replaced by spaces. If it's solely up to you to choose tabs or spaces then it's your choice.

Tabs allow you to configure any number of spaces for one tab e.g. you can configure 1 tab = 4 spaces or 2 spaces but that's what many programmers will not like. Why? because when you copy paste the code from one editor to another e.g. from Eclipse to notepad or TextPad, you will suddenly see the tabs are replaced by more or less number of spaces and code is looking much wider or narrower than what it looks on Eclipse IDE.

In short, tabs hide the true spacing. If you use spaces, you know for sure that your program will have same spacing no matter which text editor you copy paste them. So, the problem with using tabs are the inconsistent number of spaces, one can choose two spaces width tabs and other programmers can choose 8 spaces width tabs.



Eclipse settings to use spaces instead of tabs in Java 

You can follow below steps to change eclipse settings to use spaces instead of tabs, Eclipse by default use tabs to format Java code.
  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 and Apply the changes.


Here is the diagram to guide you how to change the setting to use spaces instead of tabs in Java code editor:

Eclipse Settings to use Spaces Instead of tabs


Just make sure you give a new name to Setting as Eclipse will not allow you to change the built-in settings as shown below.

How to make Eclipse use spaces instead of tabs in Java editor



Here is how your code will look like before and after when you copy it to a text editor e.g. VIM editor or Emacs editor:

Before
public static float root(int number) {
        float root = 0.0f;
        float square = root;
        while (square <= number) {
            root++;
            square = root * root;
        }
        return root;
    }
You can see the code has more spacing that what it had in Eclipse because the default setting for Eclipse was using 4 space width for tabs. When you copy the code to VIM editor or any other UNIX editor e.g emacs the tab is replaced with 8 spaces for indentation, the result will look like following, much wider code.

After

        public static float root(int number) {
                float root = 0.0f;
                float square = root;
                while (square <= number) {
                        root++;
                        square = root * root;
                }
                return root;
        }

Though make sure you apply new indentation setting to existing Java files by first selecting all lines using Ctrl + A and then Ctrl+I to apply current indentation setting. Otherwise, existing tabs will prevent spaces in new lines created with the Enter key.

It also replaces existing tabs with spaces. This is an important step many Eclipse user forget and think that setting is not working properly. So make sure you do this with your existing Java file before copying text to the text editor. You can also see Eclipse in Action: A Guide for the Java Developers, to learn more about hidden features of Eclipse IDE.

How to use space instead of tabs in Eclipse IDE


Btw, if you are using Eclipse to C/C++ developer, writing XML, HTML or CSS file, make sure you go their respective editor and change the corresponding indentation setting to use space instead of tab. These steps will only change the indentation settings for Java file in Eclipse, they will not change space setting for C/C++, HTML or XML files.

Related Eclipse IDE tutorials for Java Programmers
  • 30 Useful Eclipse Shortcuts for Java Developers (list)
  • 10 Useful tips to debug Java program in Eclipse (tips)
  • How to remote debug a Java Program in Eclipse and Linux (guide)
  • How to attach Java source code to JAR file in Eclipse? (guide)
  • How to create an executable JAR file from Eclipse? (example)
  • 3 Books to Learn Eclipse IDE for Java developers (list)



No comments :

Post a Comment