Wednesday, May 17, 2023

How use Spaces instead of Tabs in Eclipse Java editor? Example

I use Eclipse IDE extensively to write Java programs for testing and example purposes, but when I copy those programs 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 treats 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.

Btw, if you are a beginner, I suggest you first go through these Eclipse online courses to understand the core concepts of Eclipse IDE and get yourself familiar with UI and essential features. Learning plugins will be a lot easier after that.





Tabs or Spaces in Java Eclipse? 

The tabs vs space war are is as old as God vs Demons but for me, it's 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 like 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 fewer 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 the same spacing no matter which text editor you copy-paste them. So, the problem with using tabs is the inconsistent number of spaces, one can choose two spaces width tabs and other programmers can choose 8 spaces width tabs.

How use Spaces instead of Tabs in Eclipse Java editor? Example





Eclipse settings to use spaces instead of tabs in Java 

You can follow the 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 on how to change the setting to use spaces instead of tabs in the 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 than 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 the 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 a new indentation setting to existing Java files by first selecting all lines using Ctrl + A and then Ctrl+I to apply the 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 users forget and think that the 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 these Eclipse books and courses, 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 files, make sure you go to their respective editor and change the corresponding indentation setting to use space instead of a tab. These steps will only change the indentation settings for Java files in Eclipse, they will not change space settings 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 the 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