Instantly show a class/file in the Package/Project Explorer in Eclipse

While working on a class or XML file, you’ll often find that you want to work with the actual .class or .xml file in the Package Explorer, Project Explorer or Navigator views. You may want to copy, move or delete the file or do stuff that are only accessible in these views (eg. browse all files in the same folder).

Eclipse has a feature called Show In, that’s almost like a Quick Open. It instantly navigates to the current editor’s file in a requested view and positions you directly on the file in that view, no matter how deep in the hierarchy the file is. It’s also keyboard friendly, which means no more reaching for the mouse.

This feature is perfect for those (like me) who don’t like the Link with Editor functionality on certain views. I don’t like the Package Explorer to Link with Editor as I like to know things are the way I left them (see the rant at the end of the post).

Continue reading

Select entire strings and methods in Eclipse with a single keystroke

Selecting strings is a common thing especially if you want to (a) extract a constant representing it, (b) replace the string with a message bundle key or (c) use it as a search term to look for the same string across the codebase. When refactoring, you’ll sometimes want to delete a method or move it around, requiring you to select the entire method.

The problem is that selecting these can be slow. To select a string you’ll typically move the cursor to the start of the string then select the string using the arrow keys or Ctrl+Shift+Right (select next word) repeatedly until you reach the end of the string which can be slow depending on the number of words in the string. The strategy for methods is similar, move to the start and select but using .

Eclipse has a keyboard shortcut called Select Enclosing Element to make this faster. It allows you to select entire strings from anywhere within the string and methods from anywhere within the method. A bonus is that you do the same thing no matter how big the string/method is.

Continue reading

Jump to the start and end of methods, loops, blocks and XML tags

Common wisdom says that we should keep our methods small and avoid nested if statements and nested loops. But I’ve seen some huge methods (1000’s of lines) and code that included an if in a while in a for loop in an if.

The problem is finding out where the block starts and where it finishes (ie. where it’s opening and closing brackets are). Also, most frameworks require XML configuration, leading to big XML files that become hard to navigate, eg. moving between tags.

But, once again, Eclipse makes it easy to jump between the opening and closing brackets/tags using a keystroke.

Continue reading

Move, copy and delete lines with a single keystroke

When coding new features or making big changes to existing functions, I end up moving around a lot of code by typing something, deleting it, copying another line to modify it slightly, moving another line up or down, etc. Deleting a line is also more common than you think.

Instead of selecting lines and using standard copy, paste and delete to copy, move or delete lines, I’d rather use only single keystrokes. Why? Because selecting a line is slow since you have to move the beginning to fully select it. Then you have to copy, move to the new location and paste. Also, it impresses any colleagues watching me code.

Continue reading