Friday, August 29, 2008

Keyboard Shortcuts - Eclipse

Whether you call them Keyboard Shortcuts, Key Bindings, Hotkeys, or what-have-you the Eclipse IDE contains a great number of useful keyboard shortcuts...from general navigation to coding/editing and running/debugging. In fact, beneath the surface lies an entirely extensible command and binding framework, which I have only briefly investigated.

One simple shortcut for displaying a list of these shortcuts is
  • Ctrl + Shift + L => Show Key Assist
This same feature is available in some Eclipse RCP products as well, including Lotus Notes 8. Unfortunately, I don't find such a list all that useful...especially if I am looking for one binding/command in particular. Fortunately, in the Eclipse IDE at least, hitting Ctrl+Shift+L again will open the key binding preferences page, found in Preferences under General->Keys. This page contains a long list of all the key bindings in your system and is also equipped with a fancy filtering search function which is VERY useful for finding both commands and well as letting you set your own custom bindings.

Here is a list of some of the most useful Eclipse shortcuts I use:
Disclaimer: my shortcuts are very Java(JDT)/Plug-in(PDE) focused. Similar shortcuts are probably available in CDT or other packages, especially given the ongoing work to provide JDT-like functionality via projects such as the DLTK.

There is something to be said for de facto shortcuts which work across all types of programs. Eclipse does a good job in this respect by implementing some shortcuts that are common to many programs. Such shortcuts include
  • [update: I thought Ctrl+Tab was used to tab through the open editors, but I see now the default for this command is Ctrl+F6. Another command I would find useful is a next tab command for navigating the bottom tabs found in editors such as the Plug-in Manifest Editor.]
  • Ctrl + W => Close Tab
  • Ctrl + N => New (Other...)
One of the tasks which led to my constant use of the mouse in Eclipse was the simple navigation between views/perspectives/editors/etc. While each of these tasks has its own set of commands (Alt+Shift+Q was particularly useful for me to open/navigate to different views), I recently discovered the master of all shortcuts
  • Ctrl + 3 => Quick Access
Or as I like to call it, the "find whatever I'm looking for" command. This one command will give you a filtered search for perspectives, views, wizards, command, etc. The thing that makes this command even more useful is that it caches your command choice to provide a customized list of your common commands. While I still use Ctrl+E to activate an editor and Ctrl+N to launch the New->Other... wizard (I find it tricky to filter between those options), this is definitely one of my favorite shortcuts.

Most people I know are familiar with some common navigation shortcuts such as
  • F3 => Open Declaration
  • F2 => Open Tooltip
but they are nonetheless worth mentioning. Similar functionality is available using Ctrl+click.

Eclipse provides some useful views for navigation, including the type hierarchy and outline views, but instead of letting these views clog your precious screen real-estate, you can use their quick command options instead:
  • Ctrl + O => Quick Outline (x2 to toggle show/hide inherited members)
  • Ctrl + T => Type Hierarchy (x2 to toggle between supertype/subtype hierarchies)
In additional to the normal Find/Replace (Ctrl+F) command, Eclipse provides an advanced extensible search feature, which provides customizable scoped searches for particular resources such as Java Search, Plug-in Search, and standard global search.
  • Ctrl + H => Search
Although this feature provides powerful queries for all sorts of resources, I primarily use it for doing global/workspace file searches based on text and file type. It is also worth pointing out that the file search provides a global Replace... button as well if you look for it. For Java-specific searches, I usually find myself using some other useful bindings instead:
  • Ctrl + G => Declarations in Workspace
  • Ctrl + Shift + G => References in Workspace
Somewhat related, the call hierachy view is a very useful way to figure out who calls a method
  • Ctrl + Alt + H => Call Hierarchy
Another command I use religiously, is the open resource command
  • Ctrl + Shift + R => Open Resource
Similarly, the open type and open plug-in artifact commands are very useful when you know which type of resource you are looking for.
  • Ctrl + Shift + T => open type
  • Ctrl + Shift + A => open plug-in artifact*
Eclipse also provides some useful commands for use while programming. Perhaps the best know example would be the content assist (Ctrl+Space) feature. This binding will also rotate through a list of (customizable) templates/proposals if you press it more than once.

Another prevalent shortcut is the quick fix. This command seems to get better with each version of eclipse (especially for OSGi-related issues). I also use the lesser know quick assist command for assigning statements to fields (Ctrl+2, F) and local variables (Ctrl+2, L).
  • Ctrl + 1 => Quick Fix
  • Ctrl + 2 => Quick Assist (Assign To)
Another useful editing shortcut is the formatting command
  • Ctrl + Shift + F => format
For the longest time I used Ctrl+I to fix indentation issue, in part because I found the format option too extreme. However, after investing a little time in learning the formatting preferences, I now tend to format just about everything.

Eclipse is also chock full of refactoring/source assistance. Although each of these menus is available via shortcut (Alt+Shift+T and Alt+Shift+S), my favorite refactoring shortcuts are:
  • Alt + Shift + R => Rename
  • Alt + Shift + Z => Surround With
  • Alt + Shift + M => Extract Method
Both Surround With (try/catch, loops, etc) and Extract Method are quite useful for optimizing common tasks. However, the Rename shortcut in particular has been a real time-saver for me.

Finally I would be remiss to list my favorite run/debug shortcuts, though I think these are fairly self-explanatory:
  • F11 => Run
  • Ctrl + F11 => Debug
  • F5 => Step Into
  • F6 => Step Over
  • F7 => Step Return
  • F8 => Resume
  • Ctrl + R => Run to Line
  • Ctrl + click => Step Into Method
  • Ctrl + F2 => Terminate
One thing worth noting here is that the F11 run/debug command used to always launch the previously launched app...but it will now default to whatever project is associated with the selected resource/active editor. To edit this preference, check out the Launch Operation section of Preferences->Run/Debug->Launching.

The combination of key bindings and filtering text boxes is awesomely powerful.
Learning to use Alt, Ctrl, and Shift on both sides of keyboard is vital (maybe its just me, but I previously used my left hand for all these keys...proving problematic for bindings like Alt+Shift+R).
In general, I am not a big fan of Fx (x=1-12) shortcuts as I find them awkward to type (though I do use them and have even listed a few).

Here is a slightly condensed list of the most useful bindings I've seen people miss:
  • Ctrl + Shift + L (x2) => Key Bindings Preferences
  • Ctrl + 3 => Quick Access List
  • Ctrl + O => Quick Outline (x2 to toggle show/hide inherited members)
  • Ctrl + T => Type Hierarchy (x2 to toggle between supertype/subtype hierarchies)
  • Ctrl + H => Search (File Search)
  • Ctrl + G => Declarations in Workspace
  • Ctrl + Shift + G => References in Workspace
  • Ctrl + Alt + H => Call Hierarchy
  • Ctrl + Shift + R => Open Resource
  • Ctrl + Shift + T => Open Type
  • Ctrl + Shift + F => Format
  • Alt + Shift + F1 => Plug-in Spy
  • Alt + Shift + R => Rename
  • F11 => Run
  • Ctrl + F11 => Debug
  • Ctrl + R => Run to Line
There are also a couple of custom bindings I usually set, but I will leave those for another day. There are PLENTY of commands which I left off this list, so let me know if I missed one of your favorites!

*new in Ganymede