posted by 네코냥이 2015. 4. 2. 10:15

Is there a quick way to make Eclipse put curly brace on the next line (by itself) on a block of code?

shareimprove this question

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.

shareimprove this answer
4 
You 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 6 '09 at 4:26
3 
and you can add a save action that automatically formats the file on every save. –  Michael Wiles Aug 7 '09 at 15:54
   
@MichaelWiles - Thanks for the suggestion. I had no idea that was an option. Easy to setup too. –  Peter AjtaiFeb 8 '12 at 20:22

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.

shareimprove this answer
   
I suppose I meant on a highlighted pre-written block of code to having the curly braces on the next line –  yxkAug 6 '09 at 1:46
   
Gotcha. 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 6 '09 at 10:59

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!

shareimprove this answer
   
Thanks, that's exactly what I wanted! –  Almo Jun 28 '12 at 14:14

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

shareimprove this answer

java - Eclipse and curly braces - Stack Overflow.pdf



'JAVA' 카테고리의 다른 글

[이클립스] 코드접기  (0) 2014.07.17
[이클립스] 코드(폰트크기) 줌인 확대 축소  (2) 2014.07.02
[이클립스] 단축키 설정  (0) 2014.07.02