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.

Jump to a matching bracket

Here’s how to jump between matching brackets in methods, loops, if statements and any block in general:

  1. (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.)
  2. 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.

  1. Position the cursor on the opening/closing tag you’re interested in. It can be anywhere in the tag.
  2. 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

Support Eclipse On E

4 thoughts on “Jump to the start and end of methods, loops, blocks and XML tags

    • 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.

Leave a Reply

Your email address will not be published. Required fields are marked *