Current Articles | RSS Feed
If you code in Java, chances are you've used the Eclipse integrated development environment. If you're not a Java coder, you may have used Eclipse for one of the several other languages it can support via plugins. Even so, you may not know all the tools that are available for the Eclipse power user that can rocket your productivity levels upward. Check out these tips on helpful keyboard shortcuts, setting up the editor, and navigating at speed through your code.
One note: These keyboard shortcuts work as stated for Linux and Windows environments. If you're on a Mac, substitute Cmd for Ctrl unless otherwise noted.
Even small Java projects can get unwieldy quickly, and using the mouse to invoke a search through the source tree for classes, variables, or types can take too much time. Keyboard shortcuts can get you back to coding as fast as possible.
The most important shortcut is Ctrl-T, which generates a pop-up window with the type hierarchy of the object under the cursor. If the cursor is on a specific variable, you get information about that variable; if it's on a class name, you get information about the class. Press Ctrl-T again to see super-types as well. Highlight the line you want in the window and press Enter to navigate there in the code.
If you try to open a Java (JDK) type from here, you'll get an error. Instead, to open a Java (JDK) type, use Ctrl-Shift-T; this opens types that are in the JDK, not just in your workspace. However, note that this keystroke searches on the highlighted text, not the object under the cursor.
More generally, you can search for a particular resource of any sort within your workspace by using Ctrl-R.
If you just need to navigate through your current class, instead of using the little Outline window you can hit Ctrl-O to display class members, then Ctrl-O again to show inherited members. Start typing a method or variable name to search as you type, and press Enter to go straight to that class member. Once you get used to this, you can close down the Outline window and free up a bit of screen real estate. I don't know about you, but the more of my screen I can spread my code across, the happier I am.
Finally, to search for other references in your workspace to a particular item, move the cursor to it and press Ctrl-Shift-G. The references will all show up in the search box, which is usually to be found at the bottom of your workspace.
As well as moving around within the codebase, you want to move around within the editor. Keyboard shortcuts can help you here too.
To access the list of open editor windows, press Ctrl-E. The list that pops up shows the most recently opened windows; click to expand and show them all. You can just start typing to do an incremental search of the list, which is great if you have a complex project with a lot of edit windows open.
To return to the last edit location, press Ctrl-Q (on a Mac, it's still Ctrl-Q, not Cmd-Q). You can also navigate backward through your edit history by pressing Alt+left arrow (Cmd-] on a Mac).
One helpful feature of an IDE like Eclipse is that it can highlight code problems before you get as far as compiling and testing. If you see the telltale red underline that indicates a problem in your code, you can press Ctrl-1 to pop up the quick fix menu, which suggests some common options to resolve the perceived problem. This is particularly great for adding imports. You can perform a whole bunch of quick fixes here, including creating new class variables, changing potential typos, and creating methods.
As well as fixing problems, Eclipse can help you to make your code more readable. While you're writing or editing code, chances are that at some point you'll screw up the indentation. Eclipse can fix this up for you – just select the text and press Ctrl-I. If your local coding standards don't match the Eclipse default, you can edit them by creating a new profile under Eclipse Preferences -> Java -> Code Style -> Formatter.
Another common code-tidying situation is refactoring. Though it's a bit tedious when done by hand, with the help of Eclipse it's a snap. Use Shift-Alt-R to rename a variable. (I love this shortcut; it has saved me hours with find and replace.) No more excuses for misleading variable names! Shift-Alt-L extracts code to a local variable, and Shift-Alt-M extracts it to a method. (On a Mac, use Alt-Cmd-letter for all of these.)
To comment out a block of text quickly, select it and press Ctrl-/. Hit it again to uncomment; this is a great help when you're tracking down bugs or experimenting with different ways of doing things.
Coders are often picky about the details of how their editor works – unsurprising, given how much time we spend typing. Eclipse allows you to set a lot of preferences under Eclipse Preferences -> General -> Editors -> Text Editors. You can set tab size and spaces, how the hover behaves, and various appearance color options. You can also configure the spellcheck and a selection of keybindings under Editors -> Keys, and set more preferences under Java -> Editor.
Alternatively, you can set the whole keybinding scheme to match your preferred text editor. To use Emacs-style key bindings, use Eclipse Preferences -> General -> Keys and set the scheme with the top drop-down. If you're a Vi(m) user Eclipse doesn't give you built-in support, but a couple of plugins do the trick. I use Viable, which is free to try, then $15 CAD to remove popups. In my opinion it's worth the money. Vrapper is free and many coders seem to get on well with it. viPlugin is another option.
If you're partial to a particular external editor and can't quite get Eclipse to work the way you want it to, you can set a particular file type to be opened by an external editor. Go to Eclipse Preferences -> General -> Editors -> File Associations, highlight the relevant file type, then click the Add button under Associated editors and choose your external editor. Files of your chosen type will now be opened automatically in this editor. Bear in mind that by doing this you lose a lot of the benefits of Eclipse. For certain file types the tradeoff may be worth it, but in general it's probably a better idea to fiddle with the preferences or look for a plugin to help out.
If you want to use an external editor only very occasionally, you can set up the External Tools option to send the current buffer to an external editor instance. Go to Run -> External Tools -> External Tools Configuration and create a new Program configuration. Call it something sensible, such as "Open in Vim," and enter in the Location box the path to your editor (e.g. /usr/bin/gvim). Working Directory should be ${project_loc} (use the Variables button to check out other variables), and Arguments should be ${resource_loc}. These identify the current project directory and the current resource (file). Once you're set up and you're in an editor window, use the Run -> External Tools menu to send the file to an editor. This is a useful technique for occasional use.
${project_loc}
${resource_loc}
You can check out the Eclipse help pages for more on editors.
Another massive Eclipse time-saver is templates; you can read more about them in this article.
Allowed tags: <a> link, <b> bold, <i> italics