Search code examples
eclipsecode-formattingcode-standards

Eclipse: Can you format code on save?


In Eclipse, under Windows -> Preference -> Java -> Code Style, you can define code templates for comments and code, and you can setup a code formatter.

I'm wondering if it is possible in Eclipse to have these setting take affect every time I save a source file. Basically, instead of me highlighting everything and pressing Ctrl+Shift+F, I want Eclipse to be responsible for making sure my code is formatted properly.

Is this possible and how do you set it up?

Note On Using the Auto-Format: It's probably best to choose "Format Edited Lines" as merging changes becomes very difficult when you reformat a whole file that is in source control already that was not formatted properly. Already did this to a co-worker.


Solution

    1. In the Toolbar, navigate to: WindowPreferences
    2. In Preferences, navigate to: JavaEditorSave Actions
    3. Check the following:
      • Perform the selected actions on save
      • Format source code

    This may or may not be available in previous versions of Eclipse. I know it works in:

    Version: 3.3.3.r33x_r20080129-_19UEl7Ezk_gXF1kouft<br>
    Build id: M20080221-1800