Question

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?

Était-ce utile?

La solution

  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

Autres conseils

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

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.

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.

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.

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top