Pages

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.

Click Window Preferences

Expand Java Code Style

Click Formatter

Click the Edit button

Click the Indentation tab

Under General Settings, set Tab policy to Spaces only

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:

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.

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:

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.

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.

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.