Using Eclipse Efficiently
Wednesday November 14, 2007 by Derek Young
Eclipse is a powerful tool with a lot of features. I’m always looking for tips on working more efficiently. Most of the time when I come across a page of Eclipse tips it’s just the most basic usage, like “hit F3 to go to a declaration!” I’ve tried to put together some lesser known tips and features that I see my coworkers overlooking when I stop by their cube.
If you work with interfaces a lot you’ve probably run into the issue where going to the declaration of a function brings you to the interface, not the implementation. Instead of F3, hit Ctrl-T while on the function name. You’ll see all the classes where it’s implemented. Use the arrow keys to choose the right one and hit return to go to that definition.
Remember to use Open Type (Ctrl-Shift-T) to open a class, and Open Resource to browse to a file. (Most of my coworkers seem to use only Open Resource). Although you can use Open Resource to open Java files, Open Type shows only class names making it a shorter list. Open Type and Open Resource both support wildcards. Open Type supports camel case.
Ctrl-O in a class definition brings up Quick Outline. Start typing a member name and hit return once it’s unambiguous. Combined with Open Type this is a lightning fast way to go to any method in any class.
Ctrl-F6 is a great way to jump around between open editors. The order of editors is very intuitive with most recently used at the top. I find this much better than Switch To Editor. Ctrl-F7 does the same for views and Ctrl-F8 for perspectives.
Use Shift-Alt-X T to run the unit tests in the class you’re working on. (The keystroke seems long but they are logical, Shift-Alt-X for execute, Shift-Alt-D for debug. T for test). You can really get a fast turn around time by making changes, saving the file, then Shift-Alt-X T to see if the test succeeds.
Instead of searching for all references, try searching for Write Access or Read Access references only if you only care about one type. There are usually a lot fewer writes so this can really narrow down where a variable is changed.
Instead of jumping into a reference to see what a function does, trying leaving the Declaration view open. It shows the body of any function your cursor is over without having to use any special keys so you can always glance down for reference.
I think of Quick Fix as a tool for writing code, not something that just corrects accidental errors. Instead of trying to type perfect code and using Quick Fix only when you make a mistake, try intentionally leaving out code, then using Quick Fix to add it in. For example, call a function that isn’t defined, passing in all the arguments you want to to take. Use Ctrl-1, Enter to create an empty version of the function with all the right parameter types. I also like Quick Fix for creating casts. I assign an expression to a variable without a cast, then use Quick Fix to add it in.
Instead of declaring a variable and then assiging the value of an expression to it, try just writing the expression, then use Quick Assist – Assign to Field (Ctrl-2 L) to generate the declaration. The variable name will be highlighted and the drop down gives you several reasonable alternatives to use for the variable name. Tab again to get to the type to choose an alternative. For example if you had
new ArrayList<String>() and used Assign to Field you might choose
List<String> from the list of type alternatives.
Completion on empty string works. For example if your cursor is in an empty function, Alt-/ will show some reasonable completion choices like the member names in your class.
The Rename Refactoring (Shift-Alt-R) works without having to take your fingers off the keyboard, allowing you to safely rename variables or functions quickly. Try it from the keyboard instead of the refactoring menu. Inline also works well from the keyboard (Shift-Alt-I).
To improve completion try the Content Assist – Favorites setting. Add any static imports you use frequently. You’ll be prompted for these when completing even if you haven’t imported them. Also use Type Filters to remove classes you never want to see. If I want to use
java.util.List and complete on
List I see
antlr.collections.List at the top, which is never what I want. Adding
antlr.collections.* to the type filters suppresses this.
Taking it to the next level
To get productivity increases across the board, try the Mylyn extension. It takes some getting used to but improves the UI in lots of small subtle ways—completion works better (showing interesting choices first), opening types is easier, the Package Explorer has less junk and checkins are easier when you’re working on more than one thing at a time.
Code templates can really be a time saver. Try completing on “foreach” after a collection has been declared. Most of the time you get a good loop template.
If you have other favorite, lesser known tips, feel free to add them to the comments.