Common Shortcuts in IntelliJ IDEA – IntelliJ IDEA中的常用快捷键

最后修改: 2021年 4月 2日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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.

本文探讨了我们在JetBrains的Java IDE IntelliJ IDEA中编辑、构建和运行Java应用程序所需的键盘快捷键键盘快捷键为我们节省了时间,因为我们可以将手放在键盘上,更快地完成工作。

We looked at refactoring with IntelliJ IDEA in a previous article, so we don’t cover these shortcuts here.

我们在之前的文章中探讨了用IntelliJ IDEA进行重构的问题,因此我们在这里不涉及这些快捷方式。

2. The One Shortcut

2.一条捷径

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.

如果我们只记得一个IntelliJ IDEA的快捷键,那么它一定是Help – Find Action,在Windows中是Ctrl + Shift + A,在macOS中是Shift + Cmd + A。这个快捷键会打开一个搜索窗口,里面有所有的菜单项和其他IDE操作,无论它们是否有键盘快捷键。我们可以立即键入以缩小搜索范围,使用光标键选择一个函数,并使用Enter来执行它。

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.

从现在开始,我们将把键盘快捷键列在括号里,直接放在菜单项目名称后面。如果Windows和macOS之间的快捷键不同,通常是这样,那么我们就把Windows的快捷键放在前面,把macOS的放在后面。

On macOS computers, the Alt key is typically called Option. We’ll still call it Alt in this article to keep our shortcuts brief.

在MacOS电脑上,Alt键通常被称为Option。在本文中,我们仍然称它为Alt,以保持我们的快捷方式简短。

3. Settings

3.设置

Let’s start with configuring IntelliJ IDEA and our project.

让我们从配置IntelliJ IDEA和我们的项目开始。

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 + ;).

我们在Windows中通过文件-设置Ctrl + Alt + S),在macOS中通过IntelliJ IDEA-首选项Cmd + ,)到达IntelliJ的设置。为了配置我们当前的项目,我们选择Project视图中的顶级元素。它有项目名称。然后我们可以用文件-项目结构Ctrl + Alt + Shift + S / Cmd + ;)打开其配置。

4. Navigating to Files

4.浏览到文件

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.

我们通过探索左边的项目视图来挑选文件。我们还可以通过文件-新建Alt + Insert / Cmd + N)在当前选定的位置创建新文件。要删除当前选定的文件/文件夹,我们要触发Edit – DeleteDelete / )。我们可以在Windows上用Esc,在macOS上用Project视图切换到编辑器。这方面没有菜单项。

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).

要直接打开一个类,我们使用Navigate – ClassCtrl + N / Cmd + O)。这适用于Java类和其他语言的类,如TypeScript或Dart。如果我们想打开任何文件,例如HTML或文本文件,我们使用Navigate – FileCtrl + 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.

所谓的切换器就是当前打开的文件的列表。我们只能通过其快捷键Ctrl + Tab看到切换器,因为它没有菜单项。最近打开的文件列表可以通过查看-最近Ctrl + E / Cmd + E获得。如果我们再次按下该快捷键,那么我们只看到最近更改的文件。

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 + ]).

我们用Navigate – Last Edit LocationCtrl + Shift + Backspace /Shift + Cmd + ⌫)前往我们最后修改代码的地方。IntelliJ也跟踪我们的编辑器文件位置。我们可以用导航-后退Ctrl + Alt + Left / Cmd + [)和导航-前进Ctrl + Alt + Right / Cmd + ])浏览该历史。

5. Navigating Within Files

5.在文件中导航

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).

我们用导航-文件结构Ctrl + F12 / Cmd + F12)直接跳到一个类的字段或方法。与帮助-查找操作一样,我们可以立即键入以缩小显示的成员,使用光标键选择一个成员,并使用Enter跳到该成员。如果我们想突出显示一个成员在当前文件中的使用情况,我们可以使用Edit – Find Usages – Find Usages in FileCtrl + 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).

我们通过Navigate – Declaration or UsagesCtrl + B / Cmd + B)达到基类或方法的定义。顾名思义,在基类或方法本身上调用功能会显示其使用情况。由于这是一个非常常用的功能,它有一个鼠标快捷键。Ctrl + Click在Windows上,Cmd + Click在MacOS上。如果我们需要查看项目中某个类或方法的所有用法,我们可以调用Edit – Find Usages – Find UsagesAlt + 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.

我们的代码经常调用其他方法。如果我们把光标放在方法调用的括号内,那么View – Parameter InfoCtrl + P / Cmd + P)会显示方法参数的信息。在默认的IntelliJ IDEA配置中,该参数信息在短暂的延迟后自动出现。

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.

要看到一个类型或方法的快速文档窗口,我们需要查看-快速文档Ctrl + Q / F1)。在默认的IntelliJ IDEA配置中,如果我们把鼠标光标移到类型或方法上并等待一下,快速文档会自动出现。

6. Editing Files

6.编辑文件

6.1. Changing the Code

6.1.改变代码

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.

当我们开始输入变量、方法或类型的名称时,IntelliJ IDEA通过Code – Code Completion – BasicCtrl + Space)帮助我们完成这些名称。在默认的IntelliJ IDEA配置中,这个功能也会在短暂延迟后自动启动。我们可能需要输入一个结尾的小括号,并且要在结尾处加上一个分号。Code – Code Completion – Complete Current Statement (Ctrl + Shift + Enter / Shift + Cmd + Enter) 完成我们当前的行

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().

Code – Override Methods (Ctrl + O) 让我们选择继承的方法来覆盖。而通过Code – Generate (Alt + Insert / Cmd + N),我们可以创建常见的方法,如getters、setters或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.

我们可以使用CodeSurround withCtrl + Alt + T / Alt + Cmd + T)来在我们的代码周围放置控制结构,例如一个if语句。我们甚至可以用Code – Comment with Block Comment来注释整个代码块。这就是Windows中的Ctrl + Shift + /和macOS中的Alt + Cmd + /

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).

例如,在运行之前,IntelliJ IDEA会自动保存我们的代码。我们仍然可以通过文件-保存所有Ctrl + S / Cmd + S)手动保存所有文件。

6.2. Navigating the Code

6.2.浏览代码

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.

有时,我们需要在文件中移动代码。Code – Move Statement Up (Ctrl + Shift + Up / Alt + Shift + Up) 和 Code – Move Statement Down (Ctrl + Shift + Down / Alt + Shift +Down) 对当前选中的代码进行移动。如果我们没有选择任何东西,那么当前行会被移动。同样,Edit – Duplicate Line or Selection (Ctrl + D / Cmd + D)可以复制所选代码或当前行

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.

我们可以用导航-下一个高亮显示的错误F2)和导航-上一个高亮显示的错误Shift + F2)循环浏览当前文件中的错误。如果我们把光标放在不正确的代码上并点击Alt + Enter,IntelliJ IDEA将建议进行修复。这个快捷方式没有菜单项。如果我们的代码没有错误,该快捷方式也可能建议修改。

7. Find and Replace

7.查找和替换

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).

在当前文件中查找文本,我们使用Edit – Find – FindCtrl + F / Cmd + F。要替换当前文件中的文本,我们使用Edit – Find – ReplaceCtrl + R / Cmd + R)。在这两种情况下,我们用Edit – Find – Find Next Occurrence (F3 / Cmd + G) 和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.

我们还可以通过编辑-查找-查找文件Ctrl + Shift + F / Shift + Cmd + F)找到所有文件中的文本。同样地,编辑-查找-替换文件(Ctrl + Shift + R / Shift + Cmd + R)替换我们所有文件中的文本。我们仍然可以使用F3 / Cmd + GShift + F3 / Shift + Cmd + G来移动我们的搜索结果。

8. Build and Run

8.构建和运行

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).

当我们运行我们的项目时,IntelliJ IDEA通常会自动构建我们的项目。通过Build – Build Project (Ctrl + F9 / Cmd + F9),我们手动验证我们最近的代码修改是否仍然可以编译。我们还可以用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).

要用当前的运行配置运行我们的项目,我们使用Run – Run ‘(配置名称)’Shift + F10 / Ctrl + R)。我们用Run – Run…Alt + Shift + F10 / Ctrl + Alt + R执行一个特定的运行配置。同样,我们可以用Run – Debug ‘(configuration name)’Shift + F9 / Ctrl + D)调试当前的运行配置,用Run – Debug Alt + Shift + F9 / Ctrl + Alt + D)调试任何其他运行配置。

9. Debugging

9.调试

Our project will have bugs. Debugging helps us find and fix these bugs.

我们的项目会有bug。调试帮助我们发现并修复这些错误。

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).

调试器会在断点处停止。我们用运行-查看断点Ctrl + Shift + F8 / Shift + Cmd + F8)查看当前断点。我们可以用Run – Toggle Breakpoint – Line BreakpointCtrl + 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).

当我们的代码在调试过程中遇到断点时,我们可以用Run – Debugging Actions – Step Over (F8)跨过当前行。因此,如果该行是一个方法,我们将一次性地执行整个方法。另外,我们可以用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.

调试时,我们可能想运行我们的代码,直到当前方法结束。这就是Run – Debugging Actions – Step OutShift + F8)的作用。如果我们想让我们的程序运行到光标所在的行,那么Run – Debugging Actions – Run to Cursor (Alt + F9)就可以做到这一点。如果我们想让我们的程序一直运行到遇到下一个断点,那么Run – Debugging Actions – Resume Program (F9)就能做到。

10. Git

10.指南针

Our programs typically reside in a Git repository. IntelliJ IDEA has excellent support for Git.

我们的程序通常存放在Git仓库中。IntelliJ IDEA对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.

我们有一个键盘快捷键来给我们所有可能的Git操作。Git – VCS OperationsAlt + ` / Ctrl + V。正如预期的那样,我们可以用光标选择项目,然后点击Enter来执行它们。这也是达到常用功能的一个好方法,这些功能在默认情况下没有键盘快捷键,如显示历史记录显示差异

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.

如果我们想从远程的 Git 仓库更新我们的项目,那么我们可以选择 Git – Update ProjectCtrl + T / Cmd + T)。当我们需要在Git中提交我们的修改时,那么Git – Commit Ctrl + K / Cmd + K)就可以了。要把我们的改动恢复到 Git 中的内容,我们可以使用 Git – Uncommitted Changes – RollbackCtrl + Alt + Z / Alt + Cmd + Z。而Git – PushCtrl + Shift + K / Shift + Cmd + K)将我们的改动推送到远程Git仓库。

11. Conclusion

11.结语

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.

键盘快捷键为我们节省了时间,因为我们可以将手放在键盘上,更快地完成工作。本文探讨了在IntelliJ IDEA中配置、导航、编辑、查找和替换、运行和调试程序的快捷方式。

We also looked at the shortcuts for working with Git.

我们还研究了使用Git的快捷方式。