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

Add to FacebookAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Yahoo BuzzAdd to Newsvine

Advertisement

One Response

  1. THANKS!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.