How to auto-format code in Eclipse?
Asked Answered
H

13

453

How do you auto-format code in Eclipse?

Herminiahermione answered 27/3, 2013 at 9:9 Comment(3)
how did you post a question with meeting the "quality standards". for me, it is a very simple question. i just wanted to know how you did thatPhanerozoic
It's a very simple question, but if it hasn't been answered yet then it is a question we need. So that when someone else has the same simple question they can come and find it here.Carlyn
Will auto format applies to js files?Bisectrix
H
790

On Windows and Linux : Ctrl + Shift + F

On Mac : F

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

Herminiahermione answered 27/3, 2013 at 9:9 Comment(8)
Whats the up arrow on the mac?Crocker
@SSHThis it's the Shift keyDemonolater
I have some errors in my file. Can this be a reason for the above to not work??Perplex
These shortcuts won't work if you have errors in your file. On windows at least.Recuperator
On Linux Ubuntu and Mint distros, ctrl + shift + f works as well. ThanksVeterinarian
PERFECTO, <- I have written this with a shift, btw. :DSurvance
Does anyone know where the settings for the autoformatter are stored (like a local XML file somewhere). I'd like to distribute a team-wide settings file for my group so there aren't any inconsistencies with formatting between different OSes.Hillie
The path in eclips (Version: 2021-06 (4.20.0)) is Main menu => Source=>FormatPolarization
E
209

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.

Elouise answered 27/3, 2013 at 9:18 Comment(3)
A lot of options there it's really worth looking, also good to include the auto @Override statement on saveTimmerman
@HasanAboShally, now you just press two :DChongchoo
This option can now be found in Eclipse->Preferences->Java->Editor->SaveActionsTrehalose
W
42

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

Wrecker answered 19/10, 2013 at 3:8 Comment(0)
E
22

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.

Elka answered 12/2, 2014 at 14:52 Comment(0)
C
16

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

Ceram answered 28/4, 2017 at 16:18 Comment(0)
L
14

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

Latex answered 4/10, 2013 at 14:12 Comment(1)
That's an I for "Indent".Boyhood
B
12

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

Brunette answered 31/1, 2014 at 16:58 Comment(0)
T
12

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

Ticker answered 14/11, 2015 at 4:0 Comment(0)
L
9

The secret is simple: Ctrl+Shift+F

Lynsey answered 29/4, 2019 at 19:26 Comment(0)
G
5

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);
Gambetta answered 15/1, 2019 at 11:43 Comment(0)
R
4

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.

Rosy answered 25/2, 2014 at 11:5 Comment(0)
P
4

You can do this with
Ctrl + I

or

Ctrl + Shift + F

Petrillo answered 25/1, 2017 at 14:52 Comment(0)
M
3

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

Marteena answered 29/8, 2017 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.