Sometimes, the source code needs to replace tabs with spaces in Eclipse. 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, the code is consistent across different environments.

It is always recommended to use spaces for consistency.

These tutorials show how to convert tabs into spaces in Eclipse.

How to convert tabs to spaces in Eclipse

Following are steps by steps to convert tabs to spaces.

We have different editors in Eclipse Java, default and HTML editors, etc.

  • Open Eclipse

  • Select the Windows Menu > Preferences, It opens the Preferences popup window.

  • if you want to change the Java editor, select Java, if it is following languages, The following are options - java editor - Select the Java tab and Code Style - XML editor - Select the XML tab and Code Style - HTML editor - Select the Web tab and select HTML Files - C++/C editor - Select the C/C++ tab and Code Style

    Below are steps for a Java editor

  • Go to the Java tab Select Code Style and select the Formatter option

  • Click on the Edit button

  • It opens the profile settings window

  • Select the Indentation tab

  • Change the tab policy from Tabs Only to Spaces Only

  • Click on the ok button to save the changes.

  • Click on the Apply and Close button on the preferences window to close

Please see the screenshot.

Eclipse java editor convert tabs to spaces

Steps for Eclipse default editor

  • Open Windows > Preferences, Opens Preferences window
  • Select General > Editors > Text Editors
  • Check Insert Spaces for Tabs
  • Click on the Apply and Close button on the preferences window to close

How to replace tab spaces in Eclipse Editor for a single file

Sometimes, You want to replace a tab with spaces or commas or a new line in a single file.

Here are steps

  • Open Eclipse and open the source code file in the code editor

  • Go to the Find/Replace Menu or use Ctrl + F

  • It shows a popup dialog with Find and Replace with text boxes

  • Check Regular Expressions

  • to convert tabs to spaces Provide the below values and Click the Replace or Replace All button Find:

\t

Replace with

space
  • to convert tab to comma Provide the below values and Click the Replace or Replace All button Find what:
\t

Replace with

,
  • to convert tab to comma Find what:
\t

Replace with

\r\n

Here are characters that you can use to replace tab character

\b is a backspace character
\n\r is a new line with a returning end
\t is the tab character