How to auto-format code in Eclipse?

EclipseFormattingFormatCode Formatting

Eclipse Problem Overview


How do you auto-format code in Eclipse?

Eclipse Solutions


Solution 1 - Eclipse

On Windows and Linux : Ctrl + Shift + F

On Mac : + + F

(Alternatively you can press Format in Main Menu > Source)

Solution 2 - Eclipse

Another option is to go to Window->Preferences->Java->Editor->SaveActions and check the Format source code option. Then your source code will be formatted truly automatically each time you save it.

Solution 3 - Eclipse

CTRL + SHIFT + F will auto format your code (whether it is highlighted or non highlighted).

Solution 4 - Eclipse

This can also be done at the Project Level: In the Package Explorer, right-click on the project > Properties > Java Editor > Save Actions

This might be preferable when working as a team so that everyone's code is saved with the same format settings.

Solution 5 - Eclipse

Notice: It did not format the document unless I corrected all mistakes. Check your file before pressing CTRL + SHIFT + F.

Solution 6 - Eclipse

Press: Ctrl + A or highlight the part of the code you wish to indent and then press Ctrl + I.

Solution 7 - Eclipse

Windows -> Preferences -> Java -> Editor -> save actions -> Format source code -> Format Edited lines (or) format all lines.

Some time when you work as a team, lead don't want you to format all lines of the code in a source file (Huge track changes will be there on commit). So, select 'Format Edited lines'. This will edit and format only the lines you modified.

Gubs

Solution 8 - Eclipse

You can do with the steps below

  1. press Ctr + A (windows) or cmd + A (Mac os)

  2. Ctr + I in windows or cmd + I in Mac os

It will auto format your code

Solution 9 - Eclipse

The secret is simple: Ctrl+Shift+F

Solution 10 - Eclipse

Update your IDE with the latest PDT version for better code formatting. On the main menu click Help -> Install New Software and then add the following URL in the Work with field:

http://download.eclipse.org/tools/pdt/updates/4.0.1 

When asked for the name, give it PDT4.0.1 and then move along with the update or install.

It will see if the appropriate PDT is already installed or if it is a lower version, which then would be updated.

After restarting or applying the changes go to Windows -> Preferences on the side bar and expand PHP -> Code Style. Here you will see an item named Formatter. Select it and choose the active profile for the code formating. Thats it.

The next time you format it, it will choose a format according to the chosen active profile.

Hope it helps.

Solution 11 - Eclipse

You can do this with
Ctrl + I

or

Ctrl + Shift + F

Solution 12 - Eclipse

Also note that you can also "protect" a block from being formatted with @formatter:off and @formatter:on, avoiding a reformat on a comment for example, like in:

// Master dataframe
Dataset<Row> countyStateDf = df
    .withColumn(
        "countyState",
        split(df.col("label"), ", "));

// I could split the column in one operation if I wanted:
// @formatter:off
//    Dataset<Row> countyState0Df = df
//        .withColumn(
//            "state",
//            split(df.col("label"), ", ").getItem(1))
//        .withColumn(
//            "county",
//            split(df.col("label"), ", ").getItem(0));
// @formatter:on

countyStateDf.sample(.01).show(5, false);

Solution 13 - Eclipse

right-click on the project > Properties > Java Editor > Save Actions

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHasanAboShallyView Question on Stackoverflow
Solution 1 - EclipseHasanAboShallyView Answer on Stackoverflow
Solution 2 - EclipsekostjaView Answer on Stackoverflow
Solution 3 - EclipseTabrockView Answer on Stackoverflow
Solution 4 - EclipseLimaNightHawkView Answer on Stackoverflow
Solution 5 - EclipseAmado SaladinoView Answer on Stackoverflow
Solution 6 - EclipseZakaria ouhView Answer on Stackoverflow
Solution 7 - EclipsegubsView Answer on Stackoverflow
Solution 8 - EclipseKhang DoanView Answer on Stackoverflow
Solution 9 - EclipseAGEView Answer on Stackoverflow
Solution 10 - EclipseR TView Answer on Stackoverflow
Solution 11 - EclipseMohammed AZOUZView Answer on Stackoverflow
Solution 12 - EclipsejgpView Answer on Stackoverflow
Solution 13 - EclipseMuhammad YawarView Answer on Stackoverflow