56

I downloaded some code from the Internet, and the four-space indentation is bugging the crap out of me. I tried to do a find and replace for "<4 spaces>" and replace it with "\t", but that just replaced all sets of spaces with the string "\t". How would I put a tab in the "replace" box?

3
  • 4
    It is a personal/team code style choice, but using spaces for indentation is usually considered standard. Some of the reasons are highlighted in the answer by @caleb below. I'd recommend acquiring a taste for spaces!
    – anuragw
    Commented Jan 27, 2016 at 18:42
  • @anuragw I much prefer spaces to tabs. However work teams may require tabs (mine unfortunately does). Commented Aug 26, 2016 at 17:24
  • 5
    @anuragw I wouldn't call using spaces the "standard". Both tabs and spaces are equally acceptable and widely prevalent. What's important is consistency in usage throughout a project.
    – Elliot B.
    Commented Apr 17, 2018 at 0:29

6 Answers 6

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

enter image description here

After that, run Format menu from context menu and save the file:

enter image description here

5
  • 1
    Nice. Lots more elaborate than my answer. Would it format ALL my files (or at least open ones) that way? Or just the active one? Commented Mar 11, 2014 at 22:11
  • 4
    You can also format all files under a folder. Open context menu on the folder in Package Explorer and select Source->Format menu. Commented Mar 13, 2014 at 16:46
  • 11
    Actually, you need to change Java > Code Style > Formatter options to use spaces in order to auto format using spaces. Otherwise just checking "insert tabs for spaces" won't work. Commented Oct 12, 2014 at 8:58
  • 3
    And if you look carefully you'll see that the correct answer is not highlighted, in the last picture there is also Correct Indentation right above the highlighted Format. Format will format all of your code to the current formatting settings, which may not be what you want (or it is, it's not in the question anyway). Commented Jul 28, 2017 at 8:16
  • @Maarten, the only snag is that it works only on the current line. One needs to Select All (or a portion of the text) and then do Correct Indentation.
    – Zeus
    Commented Jul 26, 2023 at 8:29
29

I figured it out. I just check the box that says "use regular expressions", then use \t.

3
  • 2
    and it works for SQL code too... because accepted answer is only for java code Commented Oct 13, 2016 at 9:57
  • Where is this box?
    – thdoan
    Commented Jan 18, 2019 at 18:21
  • @thdoan CTRL + F and then click on the .* (dot asterisk) right back after Aa and |Aa| Commented Nov 11, 2019 at 10:39
17

In answer to the second half of your question:

The reason why people like spaces more than tabs is consistency. If you have your editor set to show tabs as 4 spaces wide, and I have my editor set to show tabs as 8 spaces wide, the code we're writing will look different to each of us. That's okay until our mutual coworker forgets that we're using tabs and starts spacing his lines using 4 spaces. Now his code looks fine to you, but all the indentation is off to me.

Also, what happens when our work decides that lines should be 80 characters long, or 120 characters long? You'll happily code with 4 space tabs, and when it gets to my editor, I suddenly see some of your lines as too long.

Generally, it doesn't matter whether you use spaces or tabs, as long as you (and every person who works on your code) agrees on which to use, and how wide a tab character is.

I use 4 spaces everywhere, because it looks the same on every editor, every repository, when cated, and everywhere else.

3
  • 2
    meta.stackoverflow.com/questions/333263/… Wasn't sure about this answer, so I did research and discussed it. Commented Aug 26, 2016 at 17:43
  • a tab is one character so counts the same towards the line limit regardless the level of indentation you or your co-worker choose to display it as. As for the co-worker who breaks your code-style and uses spaces, have a word with them about why the team follows a consistent policy. Then add checkstyle etc to the build so his branch fails to build before the merging
    – dan carter
    Commented Mar 6, 2020 at 0:18
  • When working on the same project, the first thing you should do is to set tab size to the same value for all of you and also install the same code formatter for all of you. That should be mandatory for all who works on a project and no things you mentioned can happen in that case.
    – horvoje
    Commented Feb 22, 2023 at 12:49
10

Kouhei response is in the right track, but you'll need to change the options of the Java formatter if you want the auto formatter to use spaces (in the preferences, look for Java -> Code Style -> Formatter, create a new style from one of the existing one, and choose "always use spaces"). Then, Ctrl + Shift + F will remove the tabs and insert spaces.

7

To resolve the issue of tab with space in the Eclipse editor:

  1. Menu Window --> Preferences --> Java --> Code style --> Formatter

  2. Click on configure project specific settings.

    • Choose the project
  3. Check Enable project-specific settings

    • Click on New
    • Mention your profile name --> click OK.
  4. The profile page will be popped up

    • Choose Space only under tab policy label of the indentation tab.
    • Click apply and the OK.
  5. Use Ctrl + Shift + F to format a Java class which will replace tab with space.

-1

Eclipse Helios for C++ developers

Instead of changing the default text editor, change the Code Style/Edit -> see pictures

enter image description here

enter image description here

2
  • Does the second picture appear as a result of pressing the "Edit" button in the first picture? Commented Aug 28, 2016 at 9:23
  • Will this reformat existing code or is it only for new (typed-in / pasted-in) code? Commented Aug 28, 2016 at 9:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.