The fastest ways to open editors in Eclipse using the keyboard

Something you do a lot in Eclipse is open files such as classes, XML files and property files in editors. But using the mouse to hunt through the Package Explorer folder hierarchy takes a long time, especially if you forgot where the files are located. The problem gets worse the more projects and files you have in your workspace, so there must be a better way of opening editors.

Luckily, Eclipse has a number of ways to open editors easily using the keyboard. Couple these with oodles of keyboard shortcuts to navigate between editors once you’ve opened them, and you’ve got enough tools to stay away from the mouse and make coding go a lot faster. I’ll discuss some of the more efficient options and then give a summary of when to use which method.

Open Resources

Probably the most effective way to open a file in an editor is by using the Open Resource functionality. If there’s one feature you want to use every day, it’s this one.

When you press Ctrl+Shift+R, you get a dialog that allows you to search all the files currently in your workspace. You can find any files such as classes, XML, property, HTML and other files. Here’s an example:

To find a file, start typing its name and once it’s selected, just press Enter. Eclipse filters the files as you type, so you can immediately see when your file appears in the list. You can also move through the list by pressing the Up and Down arrows, even if the search box has the focus (ie. you don’t have to use Tab to move to the list).

The search box supports wildcards, so you could type *Dao to search for all files that contain the word Dao. If you don’t include wildcard characters (* and ?) then Eclipse will do a direct match of the string you enter to the files in your workspace. The search isn’t case-sensitive, though, so you don’t have to worry about camel case.

Open Types

When you press Ctrl+Shift+T, you get a dialog similar to the Open Resources dialog called the Open Types dialog. Here’s an example:

Like the Open Resource dialog it shows a list of resources that match your search text. However, it only shows Java types (eg. classes, enums, interfaces, etc). It doesn’t show any other files in the workspace such as HTML or XML files. The other difference is that it shows Java types both from your workspace and referenced libraries, including 3rd party and Java library classes.

Similar to the Open Resources dialog, this dialog also supports wildcard characters but goes one step further by supporting camel case searches. For example, if you want to search for NullPointerException, you can type NPE and Eclipse will filter the types based on their case-sensitive camel case names. Note that typing Npe will not return the same results, as the camel cases are different.

The Open Types dialog is ideal for quickly opening 3rd party classes or built-in Java classes such as ArrayList or String.

Open Declaration & Implementation (Quick Type Hierarchy)

If you press F3 (Open Declaration) while your cursor is on a Java type such as a class, method or field, Eclipse will open the type’s declaration (if it’s not opened yet) and navigate you to the definition. In the example below, pressing F3 on the method length() will open String.class and take you to the implementation of String.length().

String name = "";
if (name.length() > 0) {...}

But Open Declaration isn’t always enough. For example, if you call the method on an interface, eg. the add() method of List, F3 will take you to the interface List and position you on the add() method. But often you actually want to go to one of the implementations of add(), eg. ArrayList.add().

In that case you can view the Quick Type Hierarchy (Ctrl+T) on the method and choosing the specific implementation you want. I discuss Quick Type Hierarchy in more detail in how to find all methods that override a certain method of a class, but here’s a quick example of what you’ll see:

Quick Type Hierarchy Example

Bonus Tip: To access the mouse equivalent of these 2 features, hold down Ctrl and hover over the element you want. Eclipse will underline it, allowing you to click on the element. If the element doesn’t have additional implementations, Eclipse will take you automatically to its sole declaration. If there are one or more implementations, you’ll get a popup menu offering you the option of opening either the declaration or implementation, eg:

Quick mentions

Here are some more features that you may find useful from time to time.

  • Open Super Implementation: When you’re positioned in a subclass that overrides a superclass method, you can choose Navigate > Open Super Implementation from the application menu to go to the super’s method. You can configure a shortcut for this by looking for the command Open Super Implementation in the Keys preference dialog. Alternatively, press Alt-N, S to invoke it via menu mnemonics (although this is not as reliable as configuring a shortcut).
  • Open Type Hierarchy: The Type Hierarchy shows you a tree representing a class’s hierarchy (ie. superclasses and subclasses). Just press F4 while positioned on a class name and the Type Hierarchy view will open. From there you can move directly to certain classes and methods in the type hierarchy.
  • Recent Files: If you’ve accidentally closed a file you still need, just press Alt+F and then the number of the file as indicated on the File menu. Eg. Alt+F, 1 will take you to the most recently opened file.
  • Show In: By using shortcuts to open editors, you don’t need to rely on the Package Explorer that much anymore, so it’s a good candidate for becoming a fast view, which could save you lots of screen space. But if you want to open the current editor in the Package Explorer, just press Alt+Shift+W, P, and Eclipse will instantly navigate to the file in the Package Explorer.

Which one should I use when?

If you don’t have enough time to learn all of these, here’s a quick guide on where to start and when to use them.

  • For fast, everyday, general usage, use Open Resource. I use it to open all resources, even Java types because the list only shows classes in my workspace which makes the search go faster. Definitely worthwile to learn.
  • If you’re browsing code (eg. during a code review or to debug it) then Open Declaration and Quick Type Hierarchy are very useful especially when combined with Open Resource.
  • If you want to view class hierarchies (eg. subclasses or superclasses), use Open Type Hierarchy, Open Super Implementation or Open Quick Hierarchy. They’re not only a good way of opening resources but also of getting an idea of your class hierarchy and hence your design.
  • If you’re trying to open 3rd party classes or Java built-in classes, first try using Open Declaration (F3), because the chances are you’ll probably be in the part of the code that uses these classes. However, if you’re not anywhere near the code, use Open Type.

Also keep in mind that you can change the keyboard shortcut on any of the features above by checking out how to manage keyboard shortcuts and using the appropriate command names to search for and modify the key.

Related Tips

5 Responses

  1. […] The fastest ways to open editors in Eclipse using the keyboard […]

  2. […] mouse Eclipse offers already a lot of shortcuts.My favorites : ctrl-shift-T or ctrl-shift-R for a goto type or resources If you don’t know them well, some articles here and here and a great plugin to learn […]

  3. Your post is a huge time saver. Thanks Byron.

  4. Hey Byron,

    instead of the short cut for the most recent files (Alt+F, 1) that you might have accidentally closed, you could also use the 4th and 5 th mouse button if present. It works the same as with the browser for navigating back and forth.

    Regards,
    Kon

    • Yep, definitely a good option I hadn’t considered. Nice quick alternative to the keyboard.

Comments are closed.