This article looks at the keyboard shortcuts that we need to edit, build, and run Java applications in JetBrains’ Java IDE, IntelliJ IDEA. Keyboard shortcuts save us time because we can keep our hands on the keyboard and get things done faster.
We looked at refactoring with IntelliJ IDEA in a previous article, so we don’t cover these shortcuts here.
2. The One Shortcut
If we remember just one IntelliJ IDEA shortcut, then it must be Help – Find Action, which is Ctrl + Shift + A in Windows and Shift + Cmd + A in macOS. This shortcut opens a search window with all menu items and other IDE actions, whether they have a keyboard shortcut or not. We can immediately type to narrow our search, use the cursor keys to select a function, and use Enter to execute it.
From now on, we’ll list the keyboard shortcuts in parentheses directly behind the menu item name. If the shortcuts differ between Windows and macOS, as they usually do, then we put the Windows shortcut first and the macOS one second.
On macOS computers, the Alt key is typically called Option. We’ll still call it Alt in this article to keep our shortcuts brief.
Let’s start with configuring IntelliJ IDEA and our project.
We reach the settings of IntelliJ in Windows with File – Settings (Ctrl + Alt + S) and in macOS with IntelliJ IDEA – Preferences (Cmd + ,). To configure our current project, we select the top-level element in the Project view. It has the project name. Then we can open its configuration with File – Project Structure (Ctrl + Alt + Shift + S / Cmd + ;).
4. Navigating to Files
After the configuration, we can start coding. First, we need to get to the file we want to work on.
We pick files by exploring the Project view on the left. We can also create new files in the currently selected location with File – New (Alt + Insert / Cmd + N). To delete the currently selected file/folder, we trigger Edit – Delete (Delete / ⌫). We can switch back from the Project view to the editor with Esc on Windows and ⎋ on macOS. There is no menu item for this.
To open a class directly, we use Navigate – Class (Ctrl + N / Cmd + O). This applies to Java classes and classes in other languages, such as TypeScript or Dart. If we want to open any file instead, such as HTML or text files, we use Navigate – File (Ctrl + Shift + N / Shift + Cmd + O).
The so-called switcher is the list of currently open files. We can only see the switcher through its shortcut Ctrl + Tab as it has no menu entry. The list of recently opened files is available with View – Recent (Ctrl + E / Cmd + E). If we press that shortcut again, then we see only the recently changed files.
We go to the place of our last code changes with Navigate – Last Edit Location (Ctrl + Shift + Backspace / Shift + Cmd + ⌫). IntelliJ also tracks our editor file locations. We can navigate that history with Navigate – Back (Ctrl + Alt + Left / Cmd + [) and Navigate – Forward (Ctrl + Alt + Right / Cmd + ]).
5. Navigating Within Files
We arrived at the file we want to work on. Now we need to navigate to the right place there.
We jump to a field or method of a class directly with Navigate – File Structure (Ctrl + F12 / Cmd + F12). As with Help – Find Action, we can immediately type to narrow down the members shown, use the cursor keys to select a member, and use Enter to jump to that member. If we want to highlight a member’s usages in the current file, we use Edit – Find Usages – Find Usages in File (Ctrl + F7 / Cmd + F7).
We reach the definition of a base class or method with Navigate – Declaration or Usages (Ctrl + B / Cmd + B). As the name suggests, invoking functionality on a base class or method itself shows its usages instead. Since this is such a commonly used functionality, it has a mouse shortcut: Ctrl + Click on Windows and Cmd + Click on macOS. If we need to see all uses of a class or method in our project, we invoke Edit – Find Usages – Find Usages (Alt + F7).
Our code often calls other methods. If we put the cursor inside the method call parentheses, then View – Parameter Info (Ctrl + P / Cmd + P) reveals information on method parameters. In the default IntelliJ IDEA configuration, this parameter information automatically appears after a short delay.
To see the quick documentation window for a type or method, we need View – Quick Documentation (Ctrl + Q / F1). In the default IntelliJ IDEA configuration, the quick documentation automatically appears if we move the mouse cursor over the type or method and wait a bit.
6. Editing Files
6.1. Changing the Code
Once we arrive at the right file and the right place, we can start editing our code.
When we start to type the name of variables, methods, or types, IntelliJ IDEA helps us finish those names with Code – Code Completion – Basic (Ctrl + Space). This function also automatically launches after a brief delay in the default IntelliJ IDEA configuration. We may need to type a closing parenthesis and have to put a semicolon at the end. Code – Code Completion – Complete Current Statement (Ctrl + Shift + Enter / Shift + Cmd + Enter) finishes our current line.
Code – Override Methods (Ctrl + O) lets us pick inherited methods to overwrite. And with Code – Generate (Alt + Insert / Cmd + N), we can create common methods like getters, setters, or toString().
We can use Code – Surround with (Ctrl + Alt + T / Alt + Cmd +T) to put control structures around our code, such as an if statement. We can even comment out a whole block of code with Code – Comment with Block Comment. That is Ctrl + Shift + / in Windows and Alt + Cmd + / in macOS.
IntelliJ IDEA automatically saves our code, for instance, before running it. We can still save all files manually with File – Save all (Ctrl + S / Cmd + S).
6.2. Navigating the Code
Sometimes, we need to move code around in our file. Code – Move Statement Up (Ctrl + Shift + Up / Alt + Shift +Up) and Code – Move Statement Down (Ctrl + Shift + Down / Alt + Shift +Down) do that for the currently selected code. If we have nothing selected, then the current line is moved. Similarly, Edit – Duplicate Line or Selection (Ctrl + D / Cmd + D) duplicates either the selected code or the current line.
We can cycle through errors in the current file with Navigate – Next Highlighted Error (F2) and Navigate – Previous Highlighted Error (Shift + F2). If we put the cursor on incorrect code and hit Alt + Enter, IntelliJ IDEA will suggest fixes. There is no menu item for this shortcut. That shortcut may also suggest changes to our code if it doesn’t have errors.
7. Find and Replace
We often need to find and replace code. Here’s how we can do this in the current file or all files.
To find text in our current file, we use Edit – Find – Find (Ctrl + F / Cmd + F). To replace text in our current file, we use Edit – Find – Replace (Ctrl + R / Cmd + R). In both cases, we move through the search results with Edit – Find – Find Next Occurrence (F3 / Cmd + G) and Edit – Find – Find Previous Occurrence (Shift + F3 / Shift + Cmd + G).
We can also find text in all our files with Edit – Find – Find in Files (Ctrl + Shift + F / Shift + Cmd + F). Likewise, Edit – Find – Replace in Files (Ctrl + Shift + R / Shift + Cmd +R) replaces text in all our files. We can still use F3 / Cmd + G and Shift + F3 / Shift + Cmd + G to move through our search result.
8. Build and Run
We want to run our project when we finish coding.
When we run our project, IntelliJ IDEA typically builds our projects automatically. With Build – Build Project (Ctrl + F9 / Cmd + F9), we validate manually if our recent code changes still compile. And we can rebuild our entire project from scratch with Build – Rebuild Project (Ctrl + Shift + F9 / Shift + Cmd + F9).
To run our project with the current run configuration, we use Run – Run ‘(configuration name)’ (Shift + F10 / Ctrl + R). We execute a particular run configuration with Run – Run… (Alt + Shift + F10 / Ctrl + Alt + R). In the same vein, we can debug the current run configuration with Run – Debug ‘(configuration name)’ (Shift + F9 / Ctrl + D) and any other run configuration with Run – Debug (Alt + Shift + F9 / Ctrl + Alt + D).
Our project will have bugs. Debugging helps us find and fix these bugs.
The debugger stops at breakpoints. We view the current breakpoints with Run – View Breakpoints (Ctrl + Shift + F8 / Shift + Cmd + F8). We can toggle a breakpoint at the current line with Run – Toggle Breakpoint – Line Breakpoint (Ctrl + F8 / Cmd + F8).
When our code hits a breakpoint during debugging, we can step over the current line with Run – Debugging Actions – Step Over (F8). So if that line is a method, we’ll execute that entire method in one fell swoop. Alternatively, we can dive into the method at the current line with Run – Debugging Actions – Step Into (F7).
When debugging, we may want to run our code until the current method is finished. That’s what Run – Debugging Actions – Step Out (Shift + F8) does. If we want our program to run to the line where our cursor is, then Run – Debugging Actions – Run to Cursor (Alt + F9) accomplishes this. And if we want our program just to run until it encounters the next breakpoint, then Run – Debugging Actions – Resume Program (F9) does just that.
Our programs typically reside in a Git repository. IntelliJ IDEA has excellent support for Git.
We have one keyboard shortcut to give us all possible Git operations: Git – VCS Operations (Alt + ` / Ctrl + V). As expected, we can select items with the cursor and hit Enter to execute them. This is also a good way to reach commonly used functionality that doesn’t have keyboard shortcuts by default, such as Show History or Show Diff.
If we want to update our project from a remote Git repository, then we go for Git – Update Project (Ctrl + T / Cmd + T). When we need to commit our changes in Git, then Git – Commit (Ctrl + K / Cmd + K) is available. To revert our changes to what’s in Git, we use Git – Uncommitted Changes – Rollback (Ctrl + Alt + Z / Alt + Cmd + Z). And Git – Push (Ctrl + Shift + K / Shift + Cmd + K) pushes our changes to a remote Git repository.
Keyboard shortcuts save us time because we can keep our hands on the keyboard and get things done faster. This article looked at shortcuts for configuring, navigating, editing, finding and replacing, running, and debugging our programs in IntelliJ IDEA.
We also looked at the shortcuts for working with Git.