Common Shortcuts in Eclipse – Eclipse中的常用快捷键

最后修改: 2021年 6月 30日

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

1. Overview

1.概述

Many developers rely heavily on keyboard shortcuts to work efficiently in their IDE of choice.

许多开发者严重依赖键盘快捷键来在他们选择的IDE中高效工作。

In this tutorial, we’re going to learn about some of the common shortcuts in the popular Eclipse IDE.

在本教程中,我们将学习流行的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.

由于Mac和Windows操作系统在键位标注上存在一些差异,我们将在本文中首先列出Mac命令,然后是Windows命令。我们还应该注意,在某些键盘配置上,我们可能需要使用功能(fn)键来激活功能键。

2. Search and Navigation

2.搜索和导航

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

2.1.快速访问搜索

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:

为了打开一个普通的搜索窗口,我们可以用它来快速搜索和打开我们需要的东西,让我们用+3或Ctrl+3来打开快速访问搜索窗口:

2.2. Overall Search

2.2.整体搜索

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:

要打开整体搜索窗口,让我们使用Ctrl + H。从这个窗口,我们可以找到整个项目或整个工作区的特定值,这取决于我们选择的选项。

2.3. Open Resource

2.3 开放资源

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:

如果我们知道要打开的文件的名称,我们可以使用 + Shift + R或Ctrl + Shift + R来调出打开资源窗口。在那里,我们可以开始输入工作区中任何资源的名称。

2.4. Open Type

2.4 开放型

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:

与打开资源窗口类似,我们可以使用 + Shift + T或Ctrl + Shift + T获得打开类型窗口。在该窗口中,我们可以开始输入我们工作区中任何类或接口的名称。

2.5. Find All References to a Type

2.5.查找对一个类型的所有引用

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:

有时我们需要在我们的项目中找到一个类或接口被使用的地方。一旦我们强调了我们的类型名称,我们可以 使用 + Shift + G或Ctrl + Shift + G来找到该类型的所有引用:

2.6. Search/Search and Replace

2.6.搜索/搜索和替换

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:

我们已经学会了如何在整个工作区进行搜索,但我们经常想在当前正在编辑的文件中查找和替换文本。我们可以使用+F或Cmd+F来打开查找/替换窗口:

2.7. Go to Line

2.7.转到线

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:

在调试我们的Java代码时,我们可能有一个来自堆栈跟踪或其他来源的行号,我们想跳转到该行。我们可以通过使用+L或Ctrl L来打开Go to Line窗口,从而轻松实现这一目的。在该窗口中,我们可以直接键入我们的行号,然后转到它。

3. Editing

3.编辑工作

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

3.1.向上或向下移动代码

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.

要移动一行代码,我们只需要把光标放在那一行。然后,我们可以使用Opt +向上或向下的方向键或Alt +向上或向下来移动那一行。

First, let’s select line 24:

首先,让我们选择第24行。

Then, we can use Opt + down arrow to move the entire line down one line:

然后,我们可以使用Opt +向下箭头,将整行向下移动一行。

3.2. Content Assist

3.2.内容协助

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:

有时我们希望在记忆变量或类名时能得到一些帮助。在 Eclipse 中,我们可以使用 Ctrl + Space 来获得建议,甚至可以让我们的变量名称自动完成:

3.3. Suggestions for Fixing Problems

3.3.修复问题的建议

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:

与Content Assist类似,我们也可以要求Eclipse提出修复问题的建议。我们可以使用+1或Ctrl+1来询问这些建议:

3.4. Comments

3.4.评论

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.

在 Java 中,我们可以用 // 来注释个别的代码行,用 /* */ 来注释代码块。在Eclipse中,我们有两种注释的快捷方式可供选择。

We can comment and uncomment individual lines of code using + / or Ctrl + /:

我们可以使用 + /或Ctrl + /来注释和取消注释个别行的代码:

To create comment blocks of code, let’s use + Opt + / or Ctrl + Alt + /. We can undo block comments with + Opt + \ or Ctrl + Alt + \:

为了创建代码块的注释,让我们使用 + Opt + /或Ctrl + Alt + /。我们可以用 + Opt + \或Ctrl + Alt + \撤销代码块的评论:

3.5. Advanced Editing Options

3.5.高级编辑选项

Next, let’s open the advanced editing options window using Opt + + S or Alt + Shift + S:

接下来,l让我们用Opt + + S或Alt + Shift + S打开高级编辑选项窗口:

4. Refactoring

4.重构

Next, let’s look at some of the shortcuts we can use to refactor our code more easily.

接下来,让我们看看我们可以使用的一些快捷方式,以便更容易地重构我们的代码。

4.1. Renaming Variables

4.1.重命名变量

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:

让我们突出显示我们的变量,并使用Opt + + R或Ctrl + Shift + R来重新命名它。我们只需输入一个新的名字并点击回车就足够了,但我们也可以选择打开一个更高级的重构窗口。

4.2. Refactoring Options

4.2.重构选项

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:

如果我们想要重构选项,我们可以高亮显示一个变量、类型、甚至一个包,并使用 Opt + + T或Ctrl + Shift + T来打开一个重构选项列表。

5. Running and Debugging

5.运行和调试

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.

我们从使用IDE中获得的一个主要优势是在调试模式下运行一个应用程序。考虑到这一点,让我们最后看看我们可以使用哪些命令来运行和调试我们的应用程序。

5.1. Running/Debugging an Application

5.1.运行/调试一个应用程序

Let’s use + F11 or F11 to run our application in debug mode:

让我们使用+F11或F11,在调试模式下运行我们的应用程序:

If we don’t need the debugger, we’ll use ⌘ + Shift + F11 or Ctrl + Shift + F11 to run the last application we ran.

如果我们不需要调试器,我们将使用⌘ + Shift + F11或Ctrl + Shift + F11来运行我们最后运行的应用程序。

5.2. Debugging

5.2.调试

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.

让我们先用F6跨过一行代码。

Let’s imagine that we’ve hit our breakpoint here:

让我们想象一下,我们在这里遇到了我们的断点。

After we’ve used F6 to step over:

在我们用F6踩过之后。

Now, if we need to step into a method, we use F5 to step into the method.

现在,如果我们需要进入一个方法,我们使用F5来进入方法。

Let’s use F5 to step into the method from the previous example:

让我们用F5步入前面例子中的方法。

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.

假设我们进入了setup()方法,并决定这不是我们需要的地方。我们将使用F7的快捷键来进入方法的返回。

When we’re ready to let our application run to completion, let’s use F8 to let our application continue execution.

当我们准备让我们的应用程序运行到完成时,让我们使用F8来让我们的应用程序继续执行。

6. Conclusion

6.结语

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.

在这篇文章中,我们已经了解了Eclipse IDE中的一些常用键盘快捷键。我们涵盖了四个一般领域的常用快捷键:搜索/查找、编辑、重构和运行/调试。