134

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.

M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
Fostah
  • 11,398
  • 10
  • 46
  • 55
  • 5
    Great question, but I disagree with enabling "Format Edited Lines". If the whole team has access to the same format XML descriptor via SCM then the team should expect their files to be reformatted. In other words, the short term pain of merging is worth the benefit of consistent formatting. – Dave Dec 07 '11 at 15:37
  • 2
    @HDave agree. The code should at any time be robust against a "Format Source" on all projects. – Thorbjørn Ravn Andersen May 29 '12 at 14:36
  • @ThorbjørnRavnAndersen If everyone is using these settings, you can select "Format Edited Lines". It will save some time if your files are large. I agree with you about necessity of the code being robust against a "Format Source", but it's not the task of eclipse to make sure that this is the case, I think. – brimborium Jul 20 '12 at 09:49
  • @brimborium a political decision. Personally I believe that the closer you stay to default settings the smoother things will run. – Thorbjørn Ravn Andersen Jul 20 '12 at 09:58
  • 1
    @ThorbjørnRavnAndersen That would mean the auto-format option being turned off... ;) I mean, I always save by `Ctrl+Shift+F` followed by `Ctrl+S`, so it would be fine by me, but there are other guys who don't care about formating. This feature makes our life so much easier... – brimborium Jul 20 '12 at 10:21
  • Gah. Eclipse formats trivial behaviour-less getter/setter properties out to multiple-line functions. Since studies have shown code comprehension & productivity are closely linked to vertical-space usage, I recommend keeping trivial properties compact; 3-4 lines including comment & both getter/setter. Auto-format is for low-performing teams, not highly competent ones. – Thomas W Mar 02 '13 at 02:16
  • this has to be the worst idea ever – Oliver Watkins Feb 27 '18 at 08:29

4 Answers4

216

Under Preferences, choose Java --> Editor --> Save Actions. Check the Perform the selected actions on save, and check the Format source code box.

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
M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
Neal Swearer
  • 2,482
  • 1
  • 16
  • 11
16

I have a snapshot of the setting. Please follow the path:

In the Preferences window, choose Java --> Editor --> Save Actions.

Check the Perform the selected actions on save, and check the Format source code box.

enter image description here

M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
VedantK
  • 9,728
  • 7
  • 66
  • 71
14

I strongly recommend checking your eclipse format xml descriptor into source control. That way all members of the team can use it and you don't get to and fro reformatting battles.

serg10
  • 31,923
  • 16
  • 73
  • 94
1

If you find that you do not have a Save Actions preference under Java--> Editor, it may be because you are using an older version of Eclipse. In that case you can install the Format on save plugin from here.

Then, under Preferences, choose Java --> Format on save. Select the Run Format option under Select a code formatting action

M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72