How to Change the Formatting Defaults on Eclipse

Table of contents:

How to Change the Formatting Defaults on Eclipse
How to Change the Formatting Defaults on Eclipse
Anonim

Are you tired of continually reformatting your source code? By simply pressing the Ctrl + Shift + F keys, Eclipse will format the entire document for you. Follow the steps below to change Eclipse's AutoFormat settings.

Steps

Change the Default Format Settings in Eclipse Step 1
Change the Default Format Settings in Eclipse Step 1

Step 1. Restart Eclipse

Click the Window menu at the top of the toolbar and click Preferences.

Change the Default Format Settings in Eclipse Step 2
Change the Default Format Settings in Eclipse Step 2

Step 2. On the left side of the box, expand the Java option, then expand Code Style and finally click on Format

Change the Default Format Settings in Eclipse Step 3
Change the Default Format Settings in Eclipse Step 3

Step 3. The active profile should be set to “Eclipse [built-in]”

It is not possible to change this setting, so we should make another one by clicking on the "New …" button below.

Change the Default Format Settings in Eclipse Step 4
Change the Default Format Settings in Eclipse Step 4

Step 4. In “profile name” choose the name you prefer for your profile

“Eclipse [built-in]” should be selected in “Initialize settings with the following profile”. “Open edit dialog now” should also be selected. Now, click on “OK” to create the new formatting settings.

Change the Default Format Settings in Eclipse Step 5
Change the Default Format Settings in Eclipse Step 5

Step 5. Now, the “Profile [profile name]” window will appear

In this tab there are 8 tabs, let's see them one by one:

  • Indentation
  • Braces
  • White space
  • Blank lines
  • New lines
  • Control statements
  • Line wrapping
  • Comments

    At the bottom there will be the "OK" and "Apply" buttons. Make sure you hit the "Apply" button every time you make changes to be sure your settings are saved.

Change the Default Format Settings in Eclipse Step 6
Change the Default Format Settings in Eclipse Step 6

Step 6. See the photo on the right to see the INDENTATION tab

Indentation (paragraph) is very important for the readability of the source code. In the General Settings area, you can change the size of the card, which you can set according to your needs (for example, save space or make the cards more readable). According to the conventions of the source code, you should leave the check mark on all the boxes of the Indentation section (the Empty lines box is not important). Don't forget to click Apply.

Change the Default Format Settings in Eclipse Step 7
Change the Default Format Settings in Eclipse Step 7

Step 7. Click on the BRACES tab, use the photo on the right as a reference if needed

. The braces settings are quite simple and are based on personal preferences. Most users use the “Same Line” or “Next Line” position. You should use the same position for each option. Don't forget to click Apply.

Change the Default Format Settings in Eclipse Step 8
Change the Default Format Settings in Eclipse Step 8

Step 8. Click on the WHITE SPACE tab

Use the photo on the right as a reference. Again, this card can be changed according to your personal reading needs. There are many options to scroll through, read them all and select or deselect, according to your preferences, where you want a space to be placed (equivalent to pressing the space key once). Don't forget to look at the preview window to see the changes being applied, and click Apply often, as there are many options to save in this tab.

Change the Default Format Settings in Eclipse Step 9
Change the Default Format Settings in Eclipse Step 9

Step 9. Click on the BLANK LINES tab and use the photo on the right as a reference

This tab allows you to specify the number of blank lines before or after the various declarations. The standard value is generally 0 or 1 depending on the option. If you can use more than one blank line, you're still just wasting space. Choose the options according to your preferences. Don't forget to select Apply.

Change the Default Format Settings in Eclipse Step 10
Change the Default Format Settings in Eclipse Step 10

Step 10. Look at the photo on the right and click on the NEW LINES tab:

This tab changes aspects related purely to the user's preference, so choose the option you prefer,. Don't forget to click Apply.

Change the Default Format Settings in Eclipse Step 11
Change the Default Format Settings in Eclipse Step 11

Step 11. Again, the CONTROL STATEMENTS tab allows you to select aspects related only to your personal preferences

Refer to the photo on the right to check the boxes. The document is read easily with or without the space added after a control declaration. To limit the length of the document, leave the boxes blank. Don't forget to click Apply.

Change the Default Format Settings in Eclipse Step 12
Change the Default Format Settings in Eclipse Step 12

Step 12. Click on the LINE WRAPPING tab and see the photo on the right

In the section "Line width and indentation levels" choose the length in characters of the lines of the document after which the text will be wrapped. Again, click on each option and choose "Line wrapping policy" and "identantion policy" if you want your document not to have too much "warping" and be easy to read. Don't forget to press Apply very often, as there are many options to save.

Change the Default Format Settings in Eclipse Step 13
Change the Default Format Settings in Eclipse Step 13

Step 13. The COMMENTS tab is the last tab to configure, use the photo on the right as a reference

You should select the option that begins with "Enable …". The and other options can be selected according to your preferences. We recommend selecting all the options that allow you to remove empty lines. Don't forget to click Apply.

Change the Default Format Settings in Eclipse Step 14
Change the Default Format Settings in Eclipse Step 14

Step 14. For example, to show you how formatting works, we have included a small program (click on the photo on the right) that is badly formatted (wrong margins, extra spaces)

Change the Default Format Settings in Eclipse Step 15
Change the Default Format Settings in Eclipse Step 15

Step 15. To format the selected document, click on Source in the top toolbar and click on Format

Alternatively, you can click Ctrl + Shift + F.

Change the Default Format Settings in Eclipse Step 16
Change the Default Format Settings in Eclipse Step 16

Step 16. Again, click on the photo on the right

Here you will see that the indentation has been restored, the extra spaces have been removed and the brackets positioned as per personal preference. Your document will not necessarily look like this, as it will be formatted according to the options you have selected.

Advice

  • When you click on an option, the preview screen on the right will show you the changes before they are applied.
  • You shouldn't shrink your cards to less than 2 in size.
  • Don't be afraid to make changes, as you can always restore default settings.

Recommended: