I found that when I save a .java file in Eclipse, it helps me to reformat any indentation and line breaks. This is very annoying. How can I avoid it?


This is part of "Code Clean up". You can configure what it does in Source -> "Clean Up ..."

Apart from that, you can also configure the formatter if you want the code formatted in general but don't like the defaults. To change these settings, use Window -> Preferences -> Java -> Code Style -> Formatter.

On the last tab of the formatter, you can define on/off tags which allow to you to prevent reformatting of some code.

| improve this answer | |
  • on /off tags just allow me prevent formatting part of code... I don't want to reformat any part of codes using eclipse. Any way without setting a new formatter? – Kit Ho Jul 13 '11 at 9:15
  • As I said: Configure the code cleanup. Just remove "Format Source Code" from the options. – Aaron Digulla Jul 13 '11 at 9:18
  • sounds it doesn't work on eclipse 3.7 is that a bug? – Kit Ho Jul 20 '11 at 15:51
  • It works for me. – Aaron Digulla Jul 21 '11 at 8:18
  • Java Editor --> save action, should also disable ! in order to make it work – Kit Ho Aug 4 '11 at 15:02

You should be able to turn off formatting by placing a special comment, like

// @formatting:off
// @formatting:on

This can be changed in the preferences

| improve this answer | |
  • You have to check "Enable On/Off tags" in the code formatter preferences for these to work. – Noumenon Feb 25 at 23:00

You could disable you Save actions if you have any.

Go to Window > Preferences and follow Java > Editor > Save actions. Then uncheck the tick for Perform the selected actions on save.

| improve this answer | |
  • Would code clean up and formatter influence it? – Kit Ho Aug 4 '11 at 15:02

I think I found the answer for my own question.

Project-> Preferences->Java Editor-> Save Action- >uncheck Format source code

This will disable all the source code format.

There are some answers saying that

Project ->Preferences->Java Code style-> Formatter (disable)
Project ->Preferences->Java Code style-> Clean up (disable)

Both both are only the setting that tell how the code be formatted It will still format the source code even if you uncheck the Formatter and Clean UP but check the Editor Save action

Java Editor will override all the source code settings.

| improve this answer | |

See https://stackoverflow.com/a/5466000/125617

There is another solution that you can use to suppress the formatting of specific block comments. Use /*- (note the hyphen) at the beginning of the block comment, and the formatting won't be affected if you format the rest of the file.

 * Here is a block comment with some very special
 * formatting that I want indent(1) to ignore.
 *    one
 *        two
 *            three

Source: http://java.sun.com/docs/codeconv/html/CodeConventions.doc4.html

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.