Using Eclipse Better
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
PerCoT
will take you toPersonControllerTest
?
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.assertEquals
with a static import of theorg.junit.Assert.assertEquals
and a call toassertEquals
. - 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 ): 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
java.util.List
, notorg.hibernate.mapping.List
or (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.
Comments:
Tomas - Feb 18, 2010
Regarding the F3 tips (i.e. go to definition) you will jump into the interface (of course, assuming the type of the clicked method is an interface), but for interfaces I think it is more common to want to jump into the implementation, and if you use the plugin below, then you can jump directly into the implementation if there is only one, but if there are many implementations, you will get a dropdown list and can select the desired implementation.
http://eclipse-tools.sourceforge.net/implemento…
[Rune Flobakk] - Feb 18, 2010
Amen! This is a tool that I’ve found invaluable to learn shortcuts in Eclipse: http://www.mousefeed.com
It shows a baloon tip each time you do an operation with the mouse, which also has an assigned keyboard shortcut. This is all good, but it becomes really useful once you set it to actually block the operation you just tried to use the mouse for, so it forces you to use the keyboard shortcut, and thus drilling it into your fingers. Probably somewhat annoying the first one or two days, but trust me, it is worth it.
Some additional recommended shortcut assignments:
- Rerun last JUnit test: Alt-Shift-X, Z
Great for TDD and refactoring. You do small changes and frequently execute the test(s) without needing to change your view from the code your are working on. I almost have the same habit with this shortcut as with Ctrl-S in Microsoft Word ;)
- SVN/CVS/Git/whatever Revert: Ctrl-Alt-Z. The “undo” of source control management
- Remove the Ctrl-N shortcut for the generic “New” dialog, and assign things like “Ctrl-N, C”, “Ctrl-N, T”, “Ctrl-N, I” for New Class, New JUnit Test, New Interface, etc.
[sveinhd] - Feb 22, 2010
thanks for a great post. a lot of shotcuts I didn’t know
Ruben Badaró - Mar 22, 2010
I use Ctrl + O a lot to navigate the current file. For shortcuts that I don’t know, I use Ctrl + 3 - it allows me to write the command I want to execute and is really useful.
[ellnestam] - Feb 18, 2010
Type filter, Thank you!! Tip of the week, if not month. Can’t believe I’ve not seen that.
One question though? What happened to Rename - alt-shift-r. Do you always get your names right? ;-)
kartben - Feb 18, 2010
I could not live without CTRL-3 …
Thomas Ferris Nicolaisen - Feb 19, 2010
Great compilation, Johannes. Of course I have some opinions :)
Ctrl+F6 for switching editors? Bah. Just use Ctrl+PgUp/down to cycle between them. Or Ctrl+E to filter.
If you want to jump to implementation instead of interface (F3), use Ctrl+T (quick hierarchy).
I also like to bind “Rerun JUnit Test” to something around F11, I think that works better than Ctrl+F11. By default it’s not bound to a keyboard shortcut, which is silly.
Johannes Brodwall - Feb 19, 2010
alt-shift-r is a very good candidate. I wanted to keep the list short, and I expect people will discover this one on their own. ;-)