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.
Jump to a matching bracket
Here’s how to jump between matching brackets in methods, loops, if statements and any block in general:
- (Not required for Eclipse 4.2): Position the cursor after either the opening or closing bracket of the block you’re interested in. (It would’ve been nicer for Eclipse to jump to the closest bracket from any position you are, but alas it doesn’t. In fact, Eclipse 4.2 does this automatically so the wish is granted.)
- Press Ctrl+Shift+P. Eclipse takes you immediately after the matching opening/closing bracket.
Tip: This works for method argument brackets as well, useful for multiline method calls with anonymous inner classes passed as arguments.
The image below shows an example of where the cursor needs to be positioned to jump to the end of the if statement.
Jump to a matching tag in XML
Here’s how to jump between matching tags in XML.
- Position the cursor on the opening/closing tag you’re interested in. It can be anywhere in the tag.
- Press Ctrl+Shift+>. Eclipse takes you to the matching opening/closing tag.
The image below shows an example of how to jump to the closing property tag.
Related Tips
- Navigate and fix errors and warnings in a class with Eclipse keyboard shortcuts
- The easiest ways to navigate methods in a class using Eclipse keyboard shortcuts
- The fastest ways to open editors in Eclipse using the keyboard
- Compare two workspace or external files in Eclipse
- Move, copy and delete lines with a single keystroke
THANKS!
Maybe this has changed but Ctrl+Shift+P jumps for me from any position to the opening and closing bracket of the current block
Thanks Lars, I think you’re right but it’s only since 4.2 and I was writing for 3.x (see the section on bracket matching support in the New and Noteworthy for 4.2). A nice feature is that pressing it continually jumps to the closing brace, opening brace then back to your original cursor position.
I’ll update the post to mention the changes in 4.2.
Thanks for this. BTW, for anyone else on a Mac, use Cmd instead of Ctrl.