Using the Eclipse IDE every day gets you in a lot of situations, and often I find myself repeating the same actions over and over again. Here are some of the useful shortcuts and tips that I discovered myself while working with Eclipse (Warning: most of these tips assume that you have at least Eclipse IDE version 3.3 (Europa version)):
1. Find tips within Eclipse’s Help. Before continuing reading this article, you might want to check the tips provided by the Eclipse’ Help. Just go to Help > Help Contents > Workbench User Guide > Tips and tricks.
2. Let Eclipse add the cast for you. Most of the times, when dealing with methods that return instances of Object or some generic class from your hierarchy, you need to downcast the returned object to a more specific type. For example:
UserDTO user = (UserDTO)genericDAO.getGenericObject( userId );
Here, the getGenericObject() method returns an Object, which must be downcasted to the actual UserDTO instance. This downcast can be added by you (and in the case of UserDTO it is not so big deal) but you can also let Eclipse do it for you. Most of the time I do it like this: first enter
UserDTO user = genericDAO.getGenericObject( userId );
After you press Enter, Eclipse will automatically flag (depending on the settings) the line with an error regarding the requirement of the cast. Now you can press Ctrl+1 (Quick fix) and then Enter, and Eclipse will automaticaly add the cast.
Usually the QuickFix it not very useful (for me at least), but in this particular case, combined with some really long type (like some generic type that requires the actual type parameter specification) the QuickFix function can be of real big help.
3. Find your way between Eclipse’s views, perspectives, preferences and dialogs. Any view, perspective, preferences page, dialog or any other user interface unit that you interact with in Eclipse can be made active by using the Ctrl+3 shortcut. Pressing this shortcut key will bring up a small window where you can a word or some consecutive letters from the name of the view, perspective, dialog, etc that you are looking for.
4. Clean-up the code when saving compilation units. While editing code, you can use Ctrl+Shift+F to format the code (make it look pretty and stylish, according to the rules specified in Window-Preferences-Java-Code Style-Formatter), you can use Ctrl+Shift+O to organize the imports (remove any unused import, add those that are not used but not specified on the import section of the current class).However, the next step in really making Eclipse do the things for you is to configure it to perform these actions every time you save a class file. Just go to Window-Preferences- Java-Editor-Save Actions. Here you can configure all the actions that you want to perform when saving a file. This way it is enough to write the code, and hit Save. Eclipse will take care of the rest of class importing, formatting, etc.Btw, one nice article to read about some useful Eclipse shortcuts is here: Effective Eclipse: Shortcut keys.
5. Create your own shortcut for the actions that you use most. The article mentioned above gave me the idea for this 5th bullet. This is clearly the shortcut tip mother of all shortcut tips, since it allows you to create your own shortcuts for the actions that you use most. The interesting part about this tip is that, although I knew about the feature that Eclipse provided for defining custom shortcuts, I never actually thought about making things easy by defining my own custom shortcuts.As the article mentioned above depicted, it is a very good idea to create shortcuts for New Class.. dialog, Generate getters and setters dialog, Commit and Update dialogs (when working with version control systems), Switch between editors (this is accomplished by default using Ctrl+F6, but it is very non-standard from the general editors point of view, since most tabbed editors use Ctrl+Tab to swtich between tabs, so you can create this shortcut to replace the Ctrl+F6 one).
To create custom shortcuts, just go to Window-Preferences-General-Keys.
6. Use word completion in any editor type. Eclipse provides a neat feature of word completion in any editor type, not only Java file editor. If you have several editors of any type opened, start typing a word (which can be also be a long class name, for example, or some other identifier name) and use the Alt+Slash (Alt+/) shortcut to complete that word. When you press the shortcut, Eclipse will search in all the opened editors, and will collect all the identifiers that match the few letters that you typed. To cycle between options, press Alt+Slash several times.