Teddu
Teddu

Reputation: 209

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

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.

Upvotes: 8

Views: 6233

Answers (2)

Adam Burley
Adam Burley

Reputation: 6079

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.

Upvotes: 1

Luke Melaia
Luke Melaia

Reputation: 1468

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 (=).

Upvotes: 4

Related Questions