9

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?

5 Answers 5

6

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.

6
  • 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?
    – TheOneTeam
    Jul 13, 2011 at 9:15
  • As I said: Configure the code cleanup. Just remove "Format Source Code" from the options. Jul 13, 2011 at 9:18
  • sounds it doesn't work on eclipse 3.7 is that a bug?
    – TheOneTeam
    Jul 20, 2011 at 15:51
  • It works for me. Jul 21, 2011 at 8:18
  • Java Editor --> save action, should also disable ! in order to make it work
    – TheOneTeam
    Aug 4, 2011 at 15:02
4

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

2
  • 1
    You have to check "Enable On/Off tags" in the code formatter preferences for these to work.
    – Noumenon
    Feb 25, 2020 at 23:00
  • Working in vscode I found @formatter:off (s/ing/er/) to work. [heads up for those looking] Aug 23, 2021 at 23:40
2

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.

1
  • Would code clean up and formatter influence it?
    – TheOneTeam
    Aug 4, 2011 at 15:02
1

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)
or
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.

1

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


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .