Categories
General

Eclipse-ing

Thought I’d share my favourite eclipse tips, since I’ve been living in eclipse recently. I first used eclipse in 2003 on a laptop with a shoddy trackpad, and I think that lead me into a mouse-free habit.

Navigation around text:
1. ctrl-. and ctrl-, to jump to next/last compile error.
2. ctrl-shift-p to bounce between matching parens.
3. Expand/contract selection syntactically – alt+shift+up/down. Replaces 90% of all careful mouse-drag selecting.
4. Move cursor using incremental search (ctrl-j) rather than cursor keys.
5. See all uses of a field/variable quickly – bind “search all occurances in file” to a key (I used alt-shift-f), put cursor on a field, hit your shortcut, then use ctrl-. and ctrl-, to jump between occurances.
6. Move lines of code around with alt-{up,down}

Navigation around projects:
1. Switch to recently used editors/view/perspective – ctrl-{f6,f7,f8}. I also bind the f6 action to ctrl-tab too.
2. Most laptops have a “right-mouse-click” key on the keyboard.
3. “alt-shift-q p” to focus on the package explorer, shift-{left,right} to expand/collapse folders, then f12 to focus back on the editor.
4. Use ‘show call hierarchy’ (ctrl-alt-h) and ‘show in type hierarchy’ (f4) lots rather than dumb search.

Type less stuff:
1. Create new variables by just typing “new Foo();” and then “ctrl-2 l” to assign to a local.
2. In the outline view, drag methods to reorder them in the file.
3. Use templates to surround stuff – saves hunting for the correct location for a closing bracket. Create a template called “validate” with “Validate.notNull(${word_selection})”. Then use it by selecting an expression (using expand-syntactically), hit ctrl-space twice to see templates and pick your new template. Eclipse wraps the existing expression with the template! Or, do a more generic version, such as “${cursor}(${word_selection})” which sets you up to pass the selected expression as an argument to a method call.

Random:
1. Ctrl-shift-L twice takes you straight to the keybinding pref.
2. When changing existing code, try to only ever use refactorings – avoid editing at the raw textual level.
3. I also do a lot of speculative refactorings, most of which I immediately undo. For example, if I see a big method, I’ll select some likely looking chunk and try an ‘extract method’. If eclipse tells me it’ll need too many parameters, I’ll cancel and try a bigger/smaller chunk of code. Or, if one of the parameters looks odd to me, I look to see if I can reorder code, or extract a new local, prior to running extract method. I call the new method ‘foo’ until I decide if it’s a keeper or not.
4. Use the quickfixes for the if/when/switch keywords (eg. “invert if, convert to continue”)
5. Use ctrl-m to toggle maximize your view/editor.
6. Use the eclipse-fullscreen extension (ctrl-alt-z) for even more screen space.

Running stuff:
1. Go to prefs, search for “launching” and tick “always launch the last-run application”. Then use f11 and ctrl-f11 to run whatever tests/app you’re working on.

I also have an eclipse extension that I wrote to toggle between a class and its test class. I’m still squishing bugs in it, but I’ll share it if you’re interested.