Are you tired of constantly reformatting your code in Eclipse? Just press Control+Shift+F, and Eclipse will format your entire document for you. Follow these steps to customize Eclipse's formatting to your liking.
Steps

Launch Eclipse. Go to the Window menu and select Preferences.

In the Preferences window, expand the Java category on the left, then navigate to Code Style and click Formatter.

Ensure that the active profile is set to “Eclipse [built-in].” Since this setting cannot be edited, create a new one by clicking the “New...” button below.

Provide a name for the profile. Choose a name that is meaningful to you so that you can easily remember it. Make sure that the option “Initialize setting with the following profile” is set to “Eclipse [built-in],” and the “Open the edit dialog now” option is selected. Then, click “OK” to create your new format settings.

Now, you will see the “Profile ‘name you selected’” dialog box. This dialog box consists of 8 tabs, named as follows:
- Indentation
- Braces
- White space
- Blank lines
- New lines
- Control statements
- Line wrapping
- Comments
At the bottom, there are “Apply” and “OK” buttons. Remember to click “Apply” each time you make a change to ensure that it is saved and applied to your format settings.

Take a look at the INDENTATION TAB, depicted in the image on the right. Proper indentation enhances the readability of a program. In the General Settings section, you can adjust the tab size according to your preference for space-saving or better section differentiation. It’s recommended to keep all boxes in the Indent section checked (Empty lines box is optional). Don’t forget to click Apply after making changes.

Navigate to the BRACES TAB, and refer to the image on the right if needed. Brace settings are straightforward and based on personal choice. Most individuals prefer either the “Same line” or “Next line” position for braces. Ensure consistency by selecting the same position for each option. Remember to click Apply to save your changes.

Go to the WHITE SPACE TAB. Refer to the image on the right for guidance. This tab allows you to customize white spaces in your code for better readability. Review and select or deselect the options according to your preference. Make sure to check the preview window for changes and click Apply frequently due to the numerous options available in this tab.

Access the BLANK LINES TAB and utilize the provided image for assistance. Here, you can define the number of blank lines before or after various declarations. Typically, either 0 or 1 blank line is used, depending on the context. Opt for the options that suit your preference and remember to click Apply.

Refer to the image on the right and navigate to the NEW LINES TAB. This tab allows you to set preferences according to your choice. Ensure to click Apply after making your selections.

Review the CONTROL STATEMENTS TAB, which is customizable based on personal preference. Use the provided image as a guide while selecting the desired options. You can choose to add space after control statements for easier readability or leave the boxes unchecked to limit document length. Don't forget to click Apply.

Click on the LINE WRAPPING TAB and consult the image for guidance. In the “Line width and indentation levels” section, specify the document width and indentation for line wrapping. Choose appropriate “Line wrapping policy” and “Indentation policy” options to minimize line wrapping for better readability. Remember to click Apply frequently due to the abundance of options available.

Configure the options in the COMMENTS TAB. This tab is the final step in configuration, and the image on the right serves as a reference. Ensure to select options starting with “Enable...” while other options can be chosen based on personal preference. It's recommended to select all options that will “Remove blank lines.” Don't forget to click Apply after making changes.

Examine the sample program. To demonstrate how the formatting operates, there's a sample program provided (click on the image to the right) exhibiting distorted formatting such as incorrect indentation, extra spaces, etc.

Format the currently selected document. Navigate to Source in the top toolbar and then select Format, or simply use the shortcut “Control + Shift + F” to initiate formatting.

Click on the image to the right. Here, you can observe the restoration of indentation, removal of excess space, and proper placement of brackets according to user preference. Note that your document may not resemble the example exactly as it reflects your personal coding style preference.
Helpful Tips
- As you toggle settings, the preview screen on the right will display the corresponding format changes.
- It's advisable not to set a tab size smaller than 2.
- Feel free to experiment with changes, as you can always revert to the default settings.
Cautions
- If you forget to select Apply before closing the profile box, any modifications will be lost.
Required Items
- Computer
- Your own source code
- Eclipse (Version used in this article: Eclipse SDK 3.4.1)