By Fostah


2008-10-24 17:47:55 8 Comments

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.

4 comments

@VdeX 2015-06-26 06:14:08

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

@ 2009-08-10 17:14:44

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

@Neal Swearer 2008-10-24 17:53:56

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

@Vacilando 2012-03-06 13:13:41

This looks very useful -- is there a way to achieve the same for PHP code?

@ᆼᆺᆼ 2012-06-26 08:12:49

Fine, but how do you do the same for non-Java files?

@grisaitis 2012-06-29 18:42:54

@PéterVarga if you're using PyDev, check this

@HAL9000 2012-12-24 09:27:34

This looks very useful -- is there a way to achieve the same for C++ code?

@serg10 2009-03-26 10:49:35

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.

@HDave 2011-12-07 15:32:29

Can this be done so that the XML file is installed by default?

Related Questions

Sponsored Content

41 Answered Questions

[SOLVED] How can you speed up Eclipse?

  • 2008-11-25 03:18:56
  • user2427
  • 607620 View
  • 1277 Score
  • 41 Answer
  • Tags:   eclipse performance

20 Answered Questions

[SOLVED] How do I format XML in Notepad++?

29 Answered Questions

[SOLVED] How do you format code in Visual Studio Code (VSCode)

44 Answered Questions

[SOLVED] Can't start Eclipse - Java was started but returned exit code=13

13 Answered Questions

[SOLVED] How to auto-format code in Eclipse?

7 Answered Questions

23 Answered Questions

[SOLVED] Code formatting shortcuts in Android Studio for Operation Systems

17 Answered Questions

[SOLVED] "Debug certificate expired" error in Eclipse Android plugins

12 Answered Questions

5 Answered Questions

[SOLVED] How to format all Java files in an Eclipse project at one time?

Sponsored Content