Many developers rely heavily on keyboard shortcuts to work efficiently in their IDE of choice.
In this tutorial, we're going to learn about some of the common shortcuts in the popular Eclipse IDE.
We'll break the shortcuts down into four general categories:
- Search and navigation
- Editing our code
- Refactoring code
- Running and debugging
Because there are some differences in key labeling between the Mac and Windows operating systems, we'll list the Mac command first, followed by the Windows command throughout this article. We should also note that on some keyboard configurations, we may need to use the function (fn) key to activate function keys.
2. Search and Navigation
In this section, we'll cover both how to find and open resources within our codebase and how to find and jump to specific items within a single file.
2.1. Quick Access Search
To open up a general search window that we can use to quickly search and open what we need, let's use ⌘ + 3 or Ctrl + 3 to open the Quick Access Search window:
2.2. Overall Search
To open the Overall Search window, let's use Ctrl + H. From this window, we can find specific values throughout a project or the whole workspace depending on the options we choose:
2.3. Open Resource
If we know the name of the file we'd like to open, we can use ⌘ + Shift + R or Ctrl + Shift + R to bring up the Open Resource window. From there, we can start typing the name of any resource within the workspace:
2.4. Open Type
Similar to the Open Resource window, we can get the Open Type window using ⌘ + Shift + T or Ctrl + Shift + T. In that window, we can start typing the name of any class or interface in our workspace:
2.5. Find All References to a Type
Sometimes we need to find everywhere in our project that a class or interface is used. Once we've highlighted our type name, we can use ⌘ + Shift + G or Ctrl + Shift + G to find all references to that type:
2.6. Search/Search and Replace
We've learned how to search across our workspace, but we'll often want to find and replace text within the file we're currently editing. We can use ⌘ + F or Cmd + F to open the Find/Replace window:
2.7. Go to Line
When debugging our Java code, we might have a line number from a stack trace or some other source that we want to jump to. We can easily do that by using ⌘ + L or Ctrl L to open the Go to Line window. In that window, we can just type our line number and go to it:
Now that we can find our code, let's look at the shortcuts we can use to edit more efficiently.
3.1. Moving Code Up or Down
To move a line of code, we just need to have our cursor on that line. Then we can use Opt + the up or down arrow key or Alt + up or down to move that line of up or down.
First, let's select line 24:
Then, we can use Opt + down arrow to move the entire line down one line:
3.2. Content Assist
Sometimes we'd like to have a little help remembering variable or class names. In Eclipse, we can use Ctrl + Space to get suggestions or even have our variable names auto-completed:
3.3. Suggestions for Fixing Problems
Similarly to Content Assist, we can also ask Eclipse to make suggestions for fixing a problem. We can ask for these suggestions using ⌘ + 1 or Ctrl + 1:
In Java, we can comment individual lines of code using // and blocks of code using /* */. In Eclipse, we have shortcuts at our disposal for both kinds of comments.
We can comment and uncomment individual lines of code using ⌘ + / or Ctrl + /:
To create comment blocks of code, let's use ⌘ + Opt + / or Ctrl + Alt + /. We can undo block comments with ⌘ + Opt + \ or Ctrl + Alt + \:
3.5. Advanced Editing Options
Next, let's open the advanced editing options window using Opt + ⌘ + S or Alt + Shift + S:
Next, let's look at some of the shortcuts we can use to refactor our code more easily.
4.1. Renaming Variables
Let's highlight our variable and use Opt + ⌘ + R or Ctrl + Shift + R to rename it. It's sufficient for us to just type a new name and hit enter, but we can also choose to open a more advanced refactoring window:
4.2. Refactoring Options
If we want refactoring options, we can highlight a variable, type, or even a package and use Opt + ⌘ + T or Ctrl + Shift + T to open a list of refactoring options. The options displayed will vary depending on what we've highlighted:
5. Running and Debugging
One major advantage we gain from using an IDE is running an application in debug mode. With that in mind, let's finish up by looking at the commands we can use to run and debug our applications.
5.1. Running/Debugging an Application
Let's use ⌘ + F11 or F11 to run our application in debug mode:
If we don't need the debugger, we'll use ⌘ + Shift + F11 or Ctrl + Shift + F11 to run the last application we ran.
Once we've hit a breakpoint in our code, we need to decide how to move through the code from there. There are simple shortcuts for common actions.
Let's start by using F6 to step over a line of code.
Let's imagine that we've hit our breakpoint here:
After we've used F6 to step over:
Now, if we need to step into a method, we use F5 to step into the method.
Let's use F5 to step into the method from the previous example:
Let's say we get into the setup() method and decide that's not where we need to be. We'll use the F7 shortcut to Step to the method return.
When we're ready to let our application run to completion, let's use F8 to let our application continue execution.
In this article, we've learned about some of the common keyboard shortcuts available in the Eclipse IDE. We covered common shortcuts in four general areas: searching/finding, editing, refactoring and running/debugging.