How can we replace tab character with white space for existing Java code in Eclipse?
Asked Answered
M

2

8

I am using Eclipse - mars. I need to replace tab character with white spaces in existing code. I did this:

Open Window->Preferences from menu bar.
Select Text Editors from tree menu.
Uncheck Insert spaces for tabs.

But this doesn't replace for existing code. Is there any simple way to do that, considering I have some 25 java files to do this.

Meaghan answered 23/12, 2016 at 9:2 Comment(7)
Try formatting your existing code in eclipse.Signe
@LukeMelaia: How do we do that?Meaghan
#15655626Signe
seems duplicate of #408429Possibility
@Possibility He's having a problem with applying it to his java files, not changing the settings.Signe
@LukeMelaia: This solution i have tried, but this create lots of extra space in variable declaration statement. Before and after "=" operator.Meaghan
Then change your code style formatter to remove white spaces before and after assignments.Juryrigged
S
4

After you have changed your settings, you need it to be applied it to your code files. This can be done by formatting your code.

  • Windows: Ctrl + Shift + F (Control + Shift + F)
  • Mac: + Shift + F (Command + Shift + F)

  • Main menu: main menu > Source > Format

(from the answer How to auto-format code in Eclipse?)


As @StephaneM suggested:

Change your code style formatter to remove white spaces before and after assignments

To remove the whitespace before and after assignments (=).

Signe answered 23/12, 2016 at 9:29 Comment(0)
H
0

I did this:

  • Edit -> Find/Replace
  • Tick "Regular expressions"
  • In "Find" put "\t" (without quotes)
  • In "Replace with" put four spaces
  • Click "Replace All"
  • Repeat for each file

I did this because when I do "Ctrl+Shift+F" as in the accepted answer, it messed up other parts of my indentation. For example I had multiple-indented builder pattern statements (for builders which have sub-builders) and it changed them all to single-indentation.

Hispanic answered 2/8, 2022 at 11:44 Comment(1)
Replace All works only from spaces to tabs, not the other way around. Not all tabs are the same width, so it screws up column-aligned codeSchoof

© 2022 - 2024 — McMap. All rights reserved.