Navigate and fix errors and warnings in a class with Eclipse keyboard shortcuts

I really don’t like it when Eclipse shows errors/warnings annotations in a class. It’s sometimes nice to jump from one to the next and clean out a class one line at a time, but most of the time they’re just distractions, so I want to be able to find and fix them fast.

So there must be a better way to jump between the errors/warnings  than to use the mouse or page down to the next error. These methods are not only slow but often frustrating because you tend to miss the annotation, especially if it’s a big class. And navigating to the Problems view using the keyboard is ok, but sometimes overkill for just clearing out errors/warnings in one class.

A good thing then that Eclipse offers keyboard shortcuts that take you to the next/previous annotation in the class. And it does so in a way that selects the annotation immediately, allowing you to use Quick Fix (Ctrl+1) to fix it fast. So here’s how to use these shortcuts to navigate between the error/warning annotations and fix some of the errors easily.

How to jump between errors and warnings

Below are the keys you can use to go to the next/previous annotation and to initiate quick fix.

Shortcut
Description
Ctrl+. Next Annotation. Moves to the next warning/error in the class.
Ctrl+, Previous Annotation. Moves to the previous warning/error in the class.
Ctrl+1 Quick Fix. A fast way to resolve certain warnings/errors automatically, but also useful for automating common editing tasks.

To see how fast the next annotation/quick fix combo works, I’ve set up an example in the following video that shows a class with multiple errors/warnings and how using the next/previous annotation and Quick Fix combo can make you work faster. There’s 1 warning (an unused variable, message, that should die) and 2 errors (not wrapping a FileOutputStream call in a try-catch and not initialising a local variable output).

Obviously not all errors are going to be solvable by Quick Fix, but some are: Adding a missing cast, filling in types for generics and adding the @SuppressWarning or @Override annotations.  And for the rest, at least you’ll be able to get to them easily. Bonus Tip: Also see how to cleanup some of these warnings automatically every time you save.

You can, of course, remap any of these keyboard shortcuts if they’re inconvenient. Have a look at how to manage your keyboard shortcuts, specifically looking out for the commands Next, Previous (yes, for some reason they’re just called Next and Previous) and Quick Fix.

Only jumping between errors

You might sometimes want to tell Eclipse to only move between errors (and not warnings) when you press the next/previous annotation command. Well, this is controlled by a Next/Previous Annotation dropdown menu in the toolbar (as shown in the image below). As you press the next/previous annotation key (either the keyboard shortcut or toolbar button), Eclipse will move to whatever annotations are checked in the dropdown.

Eclipse’s default only has Errors and Warnings selected (which is a reasonably good default, for once). You could disable Warnings if you only want to move between Errors, or vice versa.

Bonus Tip: The other selection you might want to enable is Occurrences. An occurrence is when you stand on a variable/method and Eclipse highlights it and its declaration and usages within the class. If you select Occurrences, pressing the next/previous annotation keys will also jump to the variable’s/method’s declaration and usages. This is nice when you want to quickly move to the variable’s usage in a long class. It’s optional if you want this on, so play around and see if it works for you.

Related Tips

Support Eclipse On E

3 thoughts on “Navigate and fix errors and warnings in a class with Eclipse keyboard shortcuts

  1. The “Only jumping between errors” is the killer!
    Always been looking for a way to navigate the occurences!
    Thanks a lot