The Fastest Ways to Open Editors in Eclipse Using the Keyboard
Join the DZone community and get the full member experience.Join For Free
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.
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.
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() .
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:
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:
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.
Opinions expressed by DZone contributors are their own.