Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

5 Answers 5

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.

share|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

share|improve this answer

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.

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

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.

share|improve this answer

See http://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


share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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