When I use Eclipse I try and use shortcuts all the time. They really make me work faster and give me time to focus on getting the code out rather than slowing down to invoke IDE commands with the mouse.
Because people work differently and on different things, it’s important to manage keyboard shortcuts to suit the way you work. So it’s good that Eclipse makes it really easy to change shortcuts and also to view shortcuts for the commands you use a lot.
I’ll discuss how to change keyboard shortcuts, how to avoid conflicts with existing ones, why it’s a good thing to manage shortcuts and then end off with some examples of common shortcuts that you should add to you arsenal.
How do you manage keyboard shortcuts
The main preference page can be found under Window > Preferences > General > Keys (or faster: Press Ctrl+3, type Keys and press Enter). From here you can see all commands and assign/change their associated keyboard shortcuts. In the video, I’ll show you how to reassign a key. We’ll use this to assign Ctrl+Tab to switch to the next editor.
- You don’t have to copy a command to assign/reassign a shortcut. I just prefer to keep the old shortcut in case someone else wants to take over my keyboard and expects the shortcut to work.
- All commands registered with Eclipse are listed on the Keys preference page. Browse through them or search to see if your favourite command is listed. If it doesn’t have a shortcut assigned, then assign one immediately.
- The When dropdown on the dialog shows you in which context the command applies (eg. only when editing Java source). You can assign the same shortcut to two different commands, but if the context differs Eclipse will only execute the one command. I suggest you don’t change the context.
- Notice that Eclipse shows you which keys conflict with the selected one if their in the same context.
Examples of useful keyboard shortcuts
Before continuing the discussion, here are a couple of useful Eclipse keyboard shortcuts you should know. There are way too many shortcuts to list and you’ll come across the more useful ones in other tips.
|Ctrl+1||Quick Fix list (for resolving errors/warnings) and also Refactoring|
|Ctrl+Space||Display the autocomplete list to select a relevant method/template, etc.|
|Ctrl+3||Open Quick Access which allows you to run commands and navigate views and dialogs by searching for them, similar to Launchy on Windows or QuickSilver on Mac.|
|Ctrl+Shift+R||Open any resource in the workspace, eg. XML file, class file, etc.|
|Ctrl+Shift+T||Open a Java type, eg. a class or interface.|
|F3||Go to the declaration of the method/class/variable|
|Ctrl+Shift+F||Format the selected code. If no code is selected, format the whole class.|
|F11/Ctrl+F11||Debug/run the last launched application (see this tip for more information)|
|Ctrl+Alt+H||Display all methods that call a method (call hierarchy)|
|F2||Show JavaDoc for the current element (Shift+F2 shows external JavaDoc)|
|Alt+Up/Down, Alt+Shift+Down, Ctrl+D||Move a line up/down, copy a line, delete a line (see this tip for more information)|
|Ctrl+/||Comment/uncomment the current line or selected lines. You can be anywhere in the line, not necessarily at the beginning.|
You can press Ctrl+Shift+L to get a list of registered keyboard shortcuts in the lower right corner. But for a complete list, I prefer to go to the preference dialog as I can see all commands that are registered, even if they don’t have shortcuts registered.
But all the keys are already taken
No, they’re not. Firstly, if a key you like is already taken then be creative. Use combos like Ctrl+Alt, Alt+Shift or even Alt+Shift+Ctrl (easier to press then you think). These aren’t used as often as Ctrl or Ctrl+Shift. You can sort by Binding to see which keys are already used.
Secondly, Eclipse allows you to assign a sequence of keystrokes to a command. Eg. Alt+Shift+X, J (that’s used to run the current class as a Java application) is invoked by pressing Alt+Shift+X, releasing the keys then pressing J. If you forgot your sequence shortcuts, just press the first part (eg. Alt+Shift+X), wait a second then Eclipse helps you out by listing all commands that have this keystroke as the first part of the sequence. The list appears in the lower right corner of the window. This works for any commands assigned a sequence of keystrokes.
Here’s an example of what you might see when you press the first sequence of keystrokes (notice the list in the lower, right corner). Tip: For custom commands you want to assign, use a well-known first keystroke to group your commands. For example, I use Alt+Shift+B (B for Byron) as my grouping prefix. This way there are fewer conflicts with existing shortcuts and Eclipse can show me a quick list if I forgot which keys I used.
So why bother managing keyboard shortcuts
Managing keyboard shortcuts in Eclipse is important because:
- Not all Eclipse commands have assigned shortcuts. A command you frequently use could be assigned a shortcut. Eg. an SVN commit is registered as an Eclipse command but doesn’t have a keyboard shortcut assigned. Assigning one yourself will speed up checking in changes.
- Sometime Eclipse gets it wrong. A good example is Ctrl+F6 for switching editors – Ctrl+Tab is a much more sensible option since the keys are close together and doable with one hand.
- Going through the list of shortcuts may give you ideas for working faster or even show you a command you didn’t know Eclipse could do.
But don’t shortcuts take a long time to learn?
No ways! I often hear people complain that it takes too long to learn the shortcuts. I assure you that the 5 minutes it takes to learn the shortcut saves you hours, even days, of work over the months and years that you’ll be working on Eclipse. Here are some tips to get into and improve your keyboard usage:
- Start by learning the shortcuts for the things you do often. Are you using the mouse a lot to launch applications or select code? Well, there’s a shortcut to launch apps and one to select code faster.
- Keyboard shortcuts are often indicated next to menu items. Note them and try to use them instead of reaching for the mouse.
- Learn to punish yourself when you reach for the mouse by undoing your action and forcing yourself to use the keyboard. This seems counterproductive (and harsh) at first, but it forces your brain to rewire itself so you’ll favour the shortcuts instead of the mouse the next time.
- There is an Eclipse plugin called MouseFeed that shows you a keyboard alternative to a mouse click you performed. You can also configure it to cancel mouse clicks where a keyboard alternative is available, forcing you to use the keyboard in those instances. I don’t use it personally but what I’ve seen looks good. It definitely sounds like a good idea if you want to change your mousey ways and it relates nicely to my previous point.
Also keep this in mind: One of the easiest, surefire ways to work faster is to reduce the number of times you move your hands between the keyboard and mouse. The key is doing as much as possible with either one. For the mouse there are things like mouse gestures (if you’re on Windows, try StrokeIt). For the keyboard there are keyboard shortcuts and Eclipse has hundreds of them ready to make your life easier (also see the excellent AutoHotkey for other ways to boost your keyboard usage).
- Quick ways to navigate Eclipse editors using the keyboard
- Move, copy and delete lines with a single keystroke
- The fastest ways to navigate views in Eclipse using the keyboard
- Select entire strings and methods in Eclipse with a single keystroke
- The fastest ways to open editors in Eclipse using the keyboard
- There are many other tips with useful shortcuts, so have a look and see if some might help you.