Accepted answer

on windows and linux : ctrl + shift + f

on mac : + + f

(alternatively you can press format in main menu > source)


right-click on the project > properties > java editor > save actions


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: 

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.


you can do this with
ctrl + i


ctrl + shift + f


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
        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);


the secret is simple: ctrl+shift+f


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


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.



press: ctrl + a or highlight the part of the code you wish to indent and then press ctrl + i.


notice: it did not format the document unless i corrected all mistakes. check your file before pressing ctrl + shift + f.


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.


ctrl + shift + f will auto format your code (whether it is highlighted or non highlighted).


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.

Related Query

More Query from same tag