October 16, 2007

Effective Eclipse: II. Shortcut keys

Use your hands to write code


You should try to keep your hands on keyboard. The less you touch the mouse, the more code you can write. I am trying to keep the mouse laying still and control the IDE completely using keyboard. What do you think is faster: pressing ALT + C or right clicking the project, selecting Team -> Commit?

It is said, that if a function does not have a key binding, it is useless. Below you will find a set of essential keyboard shortcuts that I love. These shortcuts are set up by default, they should all work.

CTRL + D

Delete row. Try it! You no more need to grab the mouse and select the line, no more Home, Shift + End, Delete. Quick and clean.

ALT + Up/Down Arrow

Move the row (or the entire selection) up or down. Very useful when rearranging code. You can even select more rows and move them all. Notice, that it will be always correctly indented.


ALT + Left/Right Arrow

Move to the last location you edited. Imagine you just created a class Foo, and now you are working on a class Boo. Now, if you need to look at the Foo class, just press Alt+Left Arrow. Alt+Right Arrow brings you back to Boo.

CTRL+SHIFT+O

Organize imports. What happens when you first use a class you have not yet imported? You will see an error. But when you press this magical combination, all your missing classes will be imported, and the unused imports will vanish.

CTRL+1

Probably the most useful one. It activates the quick fix. Imagine you create a class, which implements some interface. You will get an error, because the inherited methods are not yet implemented. While you are on line where the error occurs, press this combination to activate the quick fix. Now, select the "Add unimplemented methods" option. You can use the quick fix at every error you ever receive.

Quick fix comes handy in other situations too. My favorite is the "Split variable declaration". Sometimes I need to broaden the scope of a variable. I activate the quick fix, split declaration, and use alt + arrow to put it where it belongs. You can find even more usages: Convert local variable to field, rename in file, Inline local variable..


You could use the "Split variable declaration" on the bar variable, and then move it with Alt+Arrows above the try block..


Or you could use the "Add unimplemented methods" fix here.

The best thing you can do if you see an error is to use the quick fix.

CTRL+SHIFT+T

Open Type. Imagine, that you need to have a look at the Foo class. But, where is the Foo class? Is it in the Boo project and in the foo.bar package? Or somewhere else? With this shortcut, you don't need to know. Just press it, type Foo and you are in.


CTRL+E

Shows you a list of all open editors.



CTRL+F6

Use to move between open editors. This is an slower alternative to Ctrl + E. Comes handy in a situation when you want to periodically switch between two editors, something, what is nearly impossible with Ctrl+E as it sorts entries quite randomly. Or you might just use Alt+Arrows..

CTRL+F7

Move between views. When in editor, press Ctrl+F7 to switch to the Package Explorer, or hold Ctrl and press F7 multiple times to switch to other views.

CTRL+F8

Move between perspectives. The same as previous.

CTRL + F11

Runs the application. What gets launched depends on your settings. It will either launch the last launched class (my preffered way) or it will launch currently selected resource (the default way). If you want to change its behavior read the previous post.

CTL + N

Open new type wizard. This is not very quick because you have to select the wizard type (weather you want to create new class, jsp, xml or something else) in the next step. Much faster way would be if you could just hit the shortcut and invoke the particular wizard. It is possible, just keep reading..

CTRL + M

Maximize or umaximize current tab.

CTRL + I

Corrects indentation.

CTRL + SHIFT + F

Formats code. You can make a beautiful looking code out of a mess with this. It requires a bit of setup, but it is well worth it. You can find its settings under Window->Preferences->Java->Code style->Formatter

CTRL + J

Incremental search. Similar to the search in firefox. It shows you results as you type. Don't be surprised, if you hit this combination, nothing happens - at the first glance. Just start typing and eclipse will move your cursor to the first ocurence.

CTRL + SHIFT + L

Shows you a list of your currently defined shortcut keys.

I don't like your shortcuts

Such is life nowadays. Remember, you can always change those bindings to match your preferences. Open Windows->Preferences->General->Keys. Now you can use the filter to find your shortcut and change its binding.

The real fun begins when you cannot find the command you are looking for. The key here, is to have the "Include unbounds commands" checkbox checked. It will show you all commands, even those, which have no keys bound.



While you are here, I recommend to add the following bindings:

CTRL+SHIFT+G

Bind this to "Generate getters and setters". This is a "must have".

ALT+C

Bind this to SVN/CVS "Commit".

ALT+U

Bind this to SVN/CVS "Update".
Now, type "new" (without quotes) in the filter text. You should see a list of all new type wizards. Choose the most frequently used and assign them a shortcut. For example, the most used wizard for me is the new class wizard. Thus I assigned it the CTRL+SHIFT+N keys.


Let me demonstrate a quick way to create new class now.

Hit CTRL + SHIFT + N (or the combination you assigned in the previous step). This should bring up new class wizard. Type in the name and press ALT+E. You can now select a class which will be a superclass for the newly created class. Hit ALT+A and select all implemented interfaces . Now hit ALT+F and your class will be generated. Eclipse will also provide the default implementation for all abstract and interface methods you inherited.

Did you notice the weird underscores everywhere in the dialog? They give you a hint about the shortcut key. Hit ALT and the underlined letter to press the button, check the checkbox or get focus for a textfield.


Did you notice the underscores?

I think that using shortcut keys is the fastest way to productivity and if not, then at least your wrists will say you a silent thanks. Now, don't wait, go on and assign keys to the features you use most.

One final tip from Andriy:

The problem is that there are so many keyboard shortcuts. I used to keep a printout with all the shortcuts I wanted to use. Finally I wrote an Eclipse plugin MouseFeed, which reminds the keyboard shortcuts for the actions called with mouse. You can even tell it to enforce some shortcuts - the action will run only if called with a keyboard shortcut.

So if you are struggling with yourself, if you want to use shortcuts, but always subconsciously touch the mouse, install the plugin and let it enforce the shortcuts - the mouse will be useless and you will be forced to use keyboard.

What shortcuts do you use?


25 comments:

fs said...

I didn't know about the incremental search command. Changing it to ctrl+\ makes it almost like VI. I love it, thanks.

Anonymous said...

really good article.. quite helpful

~Swaroop

Anonymous said...

Thanks for usefull shortcuts.
Here is my blog post about my favorite Eclipse shortcuts
http://voituk.kiev.ua/2007/09/24/usefull-eclipse-shortcuts/
[in Russian]

Unknown said...

also this one:
CTRL-K - 'find next'
somewhat like incremental search, but this searches for whatever text is currently selected

Naren said...

Nice effort with illustrative pictures and examples. Its tendency of “self-explorer” human nature to skip reading detailed manual/references until we hit a dead-end and more often we miss such handy tools/shortcuts or take long to learn.
Here are 1-2 page extensive list of key shortcuts if someone want to pin next to PC -

http://eclipse-tools.sourceforge.net/shortcuts.html

Anonymous said...

My favorite eclipse shortcut is CTRL+SHIFT+r or CMD+SHIFT+r on the mac. It opens up the "Open Type" window, which allows you to quickly open any file in your project, without browsing down a lengthy package structure..

Anonymous said...

Thanks for your useful tip.
Me and my friend are very very glad to read this.
(may my manager will also be happy with our more productive development)
;)

Damien Cassou said...

Don't miss ALT+SHIFT+UP which selects the smallest expression containing the current selection. As an example:

public void test() {
String name;
name = "abc".concat("def");
}

Imagine your cursor is between the d and the e of "def". Pressing ALT+SHIFT+UP selects "def". Pressing it again selects the whole right part of the instruction. Pressing it again selects the whole instruction...

You can also use ALT+SHIFT+DOWN to go in the reverse direction.

I use it on nearly all lines now. For example, to create a new Integer, I type:

new Integer(1)

then 2 times ALT+SHIFT+UP to select the whole expression, then ALT+SHIFT+L to execute the Extract Local Variable refactoring and finally ENTER to close the dialog. It prints:

Integer integer = new Integer(1);

Tomáš Kramár said...

Interesting shortcut, but for the latter example, I'd rather use a template. It should accomplish the same, with just few less strokes.

Damien Cassou said...

Can you tell us how to define a generic template for instance creation please?

Tomáš Kramár said...

In fact, this template is already defined. Try it, type new and hit ctrl+space.

Andriy said...

The problem is that there are so many keyboard shortcuts. I used to keep a printout with all the shortcuts I wanted to use. Finally I wrote an Eclipse plugin MouseFeed, which reminds the keyboard shortcuts for the actions called with mouse. You can even tell it to enforce some shortcuts - the action will run only if called with a keyboard shortcut.
I invite you to take a look at the MouseFeed plugin and let me know what you think.

Tomáš Kramár said...

Andriy,
I think that writing the plugin was great idea, however, it upsets me that it can remind only shortcuts which are already reminded.
For example, I select Navigate->Open type - and I can already see the key combination, right in the menu. It would be much better if it could remind shortcuts, which are not already visible, like ctrl+1 or ctrl+f7.
I like the way it does the reminding - the tooltip immediately hits the eyes (unlike the combination in the menu). It's useful that it can enforce the shortcuts, as it can improve the productivity greatly (after some time although).

Andriy said...

Tomáš,
thank you for the thoughtful comments. I'm currently working in the direction of making MouseFeed more intelligent. The ultimate goal is to analyze user activity and to suggest using the shortcuts which are known to improve productivity - exactly like you suggested. I also have in mind number of other optimizations besides just shortcut keys.

Tomáš Kramár said...

I am looking forward to next version :)

Andriy said...

Sometimes MouseFeed reminds the shortcuts which are not visible, e.g. when you click on a toolbar button. However in general I agree with you - the reminder by MouseFeed is just much more noticeable than the text in the menu. We got so accustomed to the menus that the shortcuts don't register in the conciseness, unless we are explicitly looking for them.

Tomáš Kramár said...

Exactly. Maybe it has something to do with the fact, that we read left-to-right. Usually, when my eyes hit the menu I am looking for, I am not bothered to look that far right. This where the plugin's strength lies - it stops me and reminds me of the more effective way.

Unknown said...

I am looking for a shortcut key to search/find the next compilation error text in a file. Now I had to use the mouse to go to next error text in the file. I am looking for a shortcut to take me to the next error text/line.

mulman said...

sripad, in Europa 3.3.2 for Mac, the short-cut keys for next/previous warning/error are Cmd-./Shift-Cmd-. It's actually moving between "annotations", and you can configure which annotations to consider for the navigation. In the key mapping preferences, the binding commands are called Next & Previous in the Navigate category.

Mohamad KASSIR said...

Thanks for this post , it is very helpfu

Anonymous said...

Nice list, additional to that I always use ctrl+shift+c to comment out code (but not to write comments with, inline).

One thing I would recommend for anyone is not to remap shortcuts over existing ones as it becomes painful when working in teams to quickly edit something on someones eclipse or vice versa - can be a source of errors when shortcut keys do not do what they are expected.

Anonymous said...

syso + CTRL + SPACE
= System.out.println()

Johannes said...

I am wondering what's CTRL + SPACE on Mac? It opens the Spotlight search all the time...

cialis said...

Interesting article, added his blog to Favorites

Anonymous said...

here is my list of useful eclipse shortcut I would rather say very useful and practical eclipse shortcut :)