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 andCode Style
- XML editor - Select theXML
tab andCode Style
- HTML editor - Select theWeb
tab and selectHTML Files
- C++/C editor - Select theC/C++
tab andCode Style
Below are steps for a Java editor
Go to the
Java
tab SelectCode Style
and select theFormatter
optionClick on the
Edit
buttonIt opens the profile settings window
Select the Indentation tab
Change the
tab policy
fromTabs Only
toSpaces 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.
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 useCtrl
+F
It shows a popup dialog with
Find
andReplace with
text boxesCheck
Regular Expressions
to convert tabs to spaces Provide the below values and Click the
Replace
orReplace All
button Find:
\t
Replace with
space
- to convert tab to comma
Provide the below values and Click the
Replace
orReplace 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