I’ve pair programmed the Java EE spike kata in Eclipse with a number of people, I’ve found that a number of keyboard short cuts and preference settings recur as useful new information. I’ve compiled the most popular ones in this article. The article is subject to change, but I won’t change the number of shortcuts.
Top five shortcuts
There are some keyboard short cuts that everyone who uses Eclipse should know:
- ctrl-1 (quick fix): You hopefully use this shortcut to get quick fix support for compiler errors and warnings. Did you know that you can also use it to assign parameters to new fields, rename variables, and invert if-statements and equals-checks? Learn to think of ctrl-1 as asking Eclipse “what can you do make the code better (or just different)?”
- ctrl-space (complete): Again, you hopefully know that you can use ctrl-space to complete the name of variables and method names. But did you know that you can type “
equa<ctrl-space>” in the class body and have Eclipse override the equals-method for you? Or that you can type “
getNam<ctrl-space>” and have Eclipse create the whole implementation of a getter for name (if there’s a name-field in the class). Or that you can type “
Test<ctrl-space>” and have Eclipse fill in the Test code template. Think of ctrl-space as asking Eclipse “guess what I’m about to write”
- ctrl-f6 (next editor): Use this to cycle between open files. It really should’ve been bound to ctrl-tab, but you can do this yourself.
- f3 (go to definition): Place the cursor on a method call or variable usage and press f3 to go to it’s definition.
- ctrl-shift-t (open type): A nifty dialog to go to any class in your project. Did you know that typing
PerCoTwill take you to
Top ten runner ups
Here are some eye-openers that people enjoy learning:
- alt-ctrl-down (copy current line): Creates a new copy of the line under the cursor on the next line. Without wiping the clipboard! Try it while selecting several lines, too
- alt-down (move current line): Moves the line under the cursor down one line. Works with alt-up, too. And with a number of lines selected. A quick way to move code around with the keyboard.
- alt-shift-left (extend selection): Progressively selects a larger syntactic element in the editor. Hard to explain. Try it out!
- ctrl-shift-m (static import): Replace a call to
Assert.assertEqualswith a static import of the
org.junit.Assert.assertEqualsand a call to
- ctrl-F11 (rerun latest command): To run for example the same test again, you can usually press ctrl-f11. Sadly, a few years back the Eclipse team tried to improve this and failed. Fix it under Windows->Preferences, Run/Debug -> Launching. Change “Launch operation” to “Always launch previous”.
- f12 (activate editor): When you perform an operation where some other pane got the focus, use f12 to return to the editor again.
- ctrl-N (new <something>): Create a new class, XML file or whatever. Be sure to use the filter
- alt-shift-l (extract local variable): My favorite refactoring. Select an expression and press alt-shift-l to assign it to a local variable and replace all uses of the expression with that variable.
- alt-shift-m (extract method): Your bread and butter refactoring to split up complex logic in understandable units.
- alt-shift-i (inline method/inline variable): The inverse of both alt-shift-l and alt-shift-m. Together, these three refactorings let you resculpt your code while being certain that the behavior is unaltered.
Top three properties to change
When I sit down with new programmers, I almost always help them make the following changes in the preferences. Find the preferences under Window->Preferences:
- Use ctrl-tab (and ctrl-shift-tab) to switch between open editors: Go to General->Keys, type in “next editor” in the filter. Select “Copy command” and type “ctrl-tab” in the Binding field. Do the same for “previous editor” and ctrl-shift-tab.
- Type filter: Do you wonder why Eclipse can’t understand that when you say List, you mean
org.hibernate.mapping.Listor (ye gods!)
java.awt.List. Well, you can make Eclipse understand. Put classes and packages you don’t like under Java->Appearance->Type filters. If your project is like mine, putting org.hibernate.mapping.*, antlr.*, java.awt.List, and com.sun.* in the list makes List unique to java.util.List. Then “organize imports” and completion works as you want.
- Static import favorites: Do you find yourself using static imports with the same few classes again and again? The preference Java->Editor->Content Assist->Favorites lets you list up classes which will have their static methods checked when you press ctrl-space to complete a method call.
org.junit.Assert.*is a good first candidate.
Learning your tool
All IDEs are rich and powerful tools. Spending some time to learn a few new tricks is well worth the effort.