my dog learned polymorphism*
The moose likes IDEs, Version Control and other tools and the fly likes How to format code (Eclipse) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "How to format code (Eclipse)" Watch "How to format code (Eclipse)" New topic
Author

How to format code (Eclipse)

amit sharma
Ranch Hand

Joined: Jul 19, 2006
Posts: 129
I get Eclipse project from my friend.The code is not properly indented .It very difficult to read the code.How can i format the code in eclipse.
Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38048
    
  22
On Eclipse, try
  • Highlight the part of the code you wish to indent
  • You can push ctrl-A to highlight all the code if you prefer
  • Push ctrl-I to indent it
  • Voila!
  • CR
    Sunil Vasudevan
    Ranch Hand

    Joined: Mar 05, 2007
    Posts: 107
    If you need to create a template for code formatting and format any class file you need then follow the steps mentioned below:

    1. Go to the Menu Windows -> Preferences.

    2. in Preferences window, navigate to Java -> Code Style -> Formatter.
    Here you have a built-in formatter provided by Eclipse. If needed, you can create your own formatting configurations.

    3. To format your code, use short cut CTRL + Shift + F


    Sunil.V<br />SCJP2, SCWCD1.4, SCBCD1.3
    Tim Holloway
    Saloon Keeper

    Joined: Jun 25, 2001
    Posts: 15960
        
      19

    There's also at least one external plug-in that you can inject into Eclipse. It's named jalopy. It is an extremely flexible java formatter with all sorts of options. Back when the built-in Eclipse code formatter was rather crude and stupid, I used it extensively.

    Jalopy is available for multiple environments. I've used it from IntelliJ and from the command line as well as inside Eclipse.


    Customer surveys are for companies who didn't pay proper attention to begin with.
    Tim Holloway
    Saloon Keeper

    Joined: Jun 25, 2001
    Posts: 15960
        
      19

    Originally posted by Campbell Ritchie:
    On Eclipse, try
  • Highlight the part of the code you wish to indent
  • You can push ctrl-A to highlight all the code if you prefer
  • Push ctrl-I to indent it
  • Voila!
  • CR

    OR, if you have a whole project full of ugly code:

    1. Multi-select the files (or directories) of ugly code.

    2. Invoke the Source/Format menu command,
     
     
    subject: How to format code (Eclipse)
     
    Similar Threads
    How do i improve variable visibility?
    war file
    Checkstyle Plugin for Eclipse
    How do I subclass View?
    Default code formatter in eclipse