Is there a quick way to make Eclipse put curly brace on the next line (by itself) on a block of code?
-
This is basically the same question as http://stackoverflow.com/questions/3704308/how-to-make-eclipse-automatically-add-braces-to-an-if-statement – David Tonhofer Feb 05 '13 at 22:53
-
See also http://stackoverflow.com/questions/9115669/java-eclipse-highlight-missing-brackets – David Tonhofer Feb 05 '13 at 22:55
6 Answers
Yes, edit your active profile (Java...Code Style...Formatter), and change the brace positions to the next line. Also, in Java..Editor..Typing, you can have it automatically insert your braces at the correct position.

- 13,631
- 10
- 59
- 101
-
I suppose I meant on a highlighted pre-written block of code to having the curly braces on the next line – worbel Aug 06 '09 at 01:46
-
1Gotcha. Yeah, the formatting suggested by the others should do the trick. I'm a ctrl-shift-f fan myself, but the menus have their place, too. ;) – Don Branson Aug 06 '09 at 10:59
For pre-written block of code, first do the settings as suggested by Don and then select that piece of code and right click Source Code->Format and the formatting would take place as per the settings done in the preferences.

- 2,895
- 2
- 22
- 22
-
4You can also use the keyboard shortcut for this, Ctrl+Shift+F, instead of the menus. When there is a selection it only formats the selection - without a selection it will format the whole file. – Joshua McKinnon Aug 06 '09 at 04:26
-
3and you can add a save action that automatically formats the file on every save. – Michael Wiles Aug 07 '09 at 15:54
-
@MichaelWiles - Thanks for the suggestion. I had no idea that was an option. Easy to setup too. – Peter Ajtai Feb 08 '12 at 20:22
Current versions of eclipse have a more convenient way of doing this.
Go to Preferences->Java->Code Style->Formatter
Click on edit, and on the new opened window, go to the Braces tab. Here you can choose how different types of blocks of code organize their curly braces.
Hope this helps any who google this, and find this post. (Like myself)
Regards to all!

- 1,865
- 1
- 17
- 26
Coming from C# background I couldn't come to grips with built in Java formatting of Eclipse.
With some effort I managed to get most of what Visual Studio used to offer me. Here is an XML version of it. You can import the file..
Preferences->Java->Code Style->Formatter->Import->Apply
Additionally see arguments on which approach is better on our sister site..
-
Oh GOD, it is nothing but plain xml. May be it complained of not trusting the website, not the content itself! – nawfal Mar 23 '13 at 18:25
-
1
-
1
The simplest and global way:
Go to Window -> Preferences.
Then in search put: "brace".
Select -> Java -> Code Style-> Formatter After opening new window go to brace tab and change it as you prefer.

- 2,214
- 6
- 30
- 53
-
I couldn't save my brace changes until I changed/made a new Profile than "Eclipse [built-in]. Don't what this is for or if my change will work. – Andrew Koper Sep 04 '13 at 19:51
-
@AndrewKoper dont create a new profile, instead type in a differnt name and export it! – Ahmad Dwaik 'Warlock' Apr 17 '14 at 09:55
In addition to the methods of changing the settings and ctrl-shift-f to apply, these settings can be done on a per project basis. Simply right-click on your project, choose properties, Java Code Style, and enable the checkbox for "enable project specific settings."
I find this invaluable for use between projects I work on to configure it to match a particular client style, along with other settings to try to match code formatting of existing code. This way, you won't upset other people but can still use your own style on other code bases. :)

- 1,887
- 2
- 20
- 24