# 重命名重构

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com.cn/help/idea/llms.txt).

> **TL;DR**
> 快捷键： `Shift+F6` (Windows), `⇧ F6` (macOS), `⇧ F6` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ R` (macOS System Shortcuts), `Shift+F6` (XWin), `Shift+F6` (GNOME), `Shift+F6` (KDE), `Shift+F6` (Emacs), `Shift+F6` (Sublime Text), `⇧ F6` (Sublime Text (macOS)), `Ctrl+R, R` (Visual Studio), `⌘ R, R` (Visual Studio (macOS)), `Ctrl+R` (NetBeans), `Alt+Shift+R` (Eclipse), `⇧ F6` (Eclipse (macOS))
>
>
>
> 主菜单：  `重构 | 重命名`
>
>
>
> 上下文菜单：  `重命名`
>
>
>
> [常规重构设置](code-editing.html):  `设置 | 编辑器 | 代码编辑`
>
>
>
> 就地重构可用
>
>
>
> IntelliJ IDEA 支持对 Java 14 代码进行此重构

使用  重命名 重构来更改符号、文件、目录、包、模块及其在整个代码中的所有引用的名称。

重命名局部变量或私有方法可以轻松地内联完成，因为只有有限的范围受影响。 重命名类或公共方法可能会影响许多文件。 在重构之前预览潜在的更改。

> **Tip:**
> 当您重命名接口中声明的方法时，您也可以查看并重命名它们的实现。 重构更改显示在   重构预览   选项卡中，位于   查找   工具窗口中。

Procedure: 使用侧边图标直接重命名代码

> **Note:**
> 使用边距图标的重命名过程不适用于 Java 文件。 在 Java 文件中重命名通过 [内嵌提示](#inplace_rename_inlay_hint)实现。

1. 在编辑器中开始重命名参数、方法或其他代码元素。 IntelliJ IDEA 将在装订区域中显示 ![重命名 图标 的用法](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.actions.suggestedRefactoringBulb.svg)。

2. 点击代码区图标或按 `Alt+Enter` (Windows), `⌥ ⏎` (macOS), `⌥ ⏎` (IntelliJ IDEA Classic (macOS)), `⌥ ⏎` (macOS System Shortcuts), `Alt+Enter` (XWin), `Alt+Enter` (GNOME), `Alt+Enter` (KDE), `Alt+Enter` (Emacs), `Alt+Enter` (Sublime Text), `⌥ ⏎` (Sublime Text (macOS)), `Alt+Enter` (Visual Studio), `⌥ ⏎` (Visual Studio (macOS)), `Alt+Enter` (NetBeans), `Ctrl+1` (Eclipse), `⌘ 1` (Eclipse (macOS)) 并应用建议。

![重命名内联](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ij_rename_inline.png)

IntelliJ IDEA 重命名代码元素，并相应地更新其用法。

![重命名重构结果](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ij_rename_result.png)

Procedure: 直接通过内嵌提示重命名代码

> **Note:**
> 内嵌提示重命名重构仅适用于 Java 文件。 其他文件可能会使用 [侧边栏图标](#inplace_rename)。

1. 在编辑器中，找到您要重命名的代码元素。

![Ij identify code element](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ij_identify_code_element.png)

2. 编辑您要重命名的代码元素的文本，IntelliJ IDEA 将显示   重命名用法  内嵌提示。

![Ij rename usages inlay hint](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ij_rename_usages_inlay_hint.png)

3. 选择内嵌提示。 如果 IntelliJ IDEA 确定某些变量可能也需要重命名，将会打开   重命名变量   对话框。 要确认重命名重构，请选择   OK  。

![Ij rename variables dialog](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ij_rename_variables_dialog.png)

如果 IntelliJ IDEA 未识别出可能也需要重命名的任何变量，IntelliJ IDEA 将立即重命名代码元素并相应地更新其用途。

![Ij code element renamed](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ij_code_element_renamed.png)

Procedure: 重命名目录或模块

1. 在 [项目工具窗口](project-tool-window.html) 中右键点击您要重命名的目录或模块。

2. 从上下文菜单中，选择  `重命名` （ `Shift+F6` (Windows), `⇧ F6` (macOS), `⇧ F6` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ R` (macOS System Shortcuts), `Shift+F6` (XWin), `Shift+F6` (GNOME), `Shift+F6` (KDE), `Shift+F6` (Emacs), `Shift+F6` (Sublime Text), `⇧ F6` (Sublime Text (macOS)), `Ctrl+R, R` (Visual Studio), `⌘ R, R` (Visual Studio (macOS)), `Ctrl+R` (NetBeans), `Alt+Shift+R` (Eclipse), `⇧ F6` (Eclipse (macOS))）。

3. 在打开的弹出窗口中，选择您想要重命名的内容。 如果您有一个内容根与模块同名的模块，您可以同时重命名它们。

![选择重构弹出窗口](https://resources.jetbrains.com.cn/help/img/idea/2026.3/select_refactoring_dialog.png)

4. 在弹出的   重命名  对话框中，输入新名称，指定其他选项和重构范围，然后点击   重构  。

> **Note:**
> 如果您同时重命名模块和目录，在 [Rename](rename-dialog-for-a-directory.html)对话框中添加新名称，检查引用搜索位置，然后点击   OK  。

Procedure: 重命名包

1. 在 [项目工具窗口](project-tool-window.html)中，点击   项目  并从列表中选择   包  。 IntelliJ IDEA 列出了您项目中的所有包。

2. 右键点击您要重命名的软件包，然后从上下文菜单中选择  `重命名`  ( `Shift+F6` (Windows), `⇧ F6` (macOS), `⇧ F6` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ R` (macOS System Shortcuts), `Shift+F6` (XWin), `Shift+F6` (GNOME), `Shift+F6` (KDE), `Shift+F6` (Emacs), `Shift+F6` (Sublime Text), `⇧ F6` (Sublime Text (macOS)), `Ctrl+R, R` (Visual Studio), `⌘ R, R` (Visual Studio (macOS)), `Ctrl+R` (NetBeans), `Alt+Shift+R` (Eclipse), `⇧ F6` (Eclipse (macOS)))。

如果您有多个与包对应的目录，或者包中包含的库中的目录无法重命名，IntelliJ IDEA 将显示警告。 但是，您可以继续选择警告中提供的选项之一。 IntelliJ IDEA 打开 [Rename](rename-dialog-for-a-package.html)对话框，您可以在其中输入新包名称并点击 [Preview](refactoring-source-code.html#refactoring_preview)以查看在您决定重构之前会影响哪些目录和文件。 如果您想撤销所做的更改，请按 `Ctrl+Z` (Windows), `⌘ Z` (macOS), `⌘ Z` (IntelliJ IDEA Classic (macOS)), `⌘ Z` (macOS System Shortcuts), `Ctrl+Z` (XWin), `Ctrl+Z` (GNOME), `Ctrl+Z` (KDE), `Ctrl+Shift+Minus` (Emacs), `Ctrl+Z` (Sublime Text), `⌘ Z` (Sublime Text (macOS)), `Ctrl+Z` (Visual Studio), `⌘ Z` (Visual Studio (macOS)), `Ctrl+Z` (NetBeans), `Ctrl+Z` (Eclipse), `⌘ Z` (Eclipse (macOS))。

Procedure: 重命名代码元素

1. 在编辑器中选择您要重命名的元素。 如果您需要重命名文件，请在   项目  工具窗口中选择一个。

2. 按 `Shift+F6` (Windows), `⇧ F6` (macOS), `⇧ F6` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ R` (macOS System Shortcuts), `Shift+F6` (XWin), `Shift+F6` (GNOME), `Shift+F6` (KDE), `Shift+F6` (Emacs), `Shift+F6` (Sublime Text), `⇧ F6` (Sublime Text (macOS)), `Ctrl+R, R` (Visual Studio), `⌘ R, R` (Visual Studio (macOS)), `Ctrl+R` (NetBeans), `Alt+Shift+R` (Eclipse), `⇧ F6` (Eclipse (macOS))，或右键点击并从上下文菜单中选择  `重命名` 。

当您调用 rename `Shift+F6` (Windows), `⇧ F6` (macOS), `⇧ F6` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ R` (macOS System Shortcuts), `Shift+F6` (XWin), `Shift+F6` (GNOME), `Shift+F6` (KDE), `Shift+F6` (Emacs), `Shift+F6` (Sublime Text), `⇧ F6` (Sublime Text (macOS)), `Ctrl+R, R` (Visual Studio), `⌘ R, R` (Visual Studio (macOS)), `Ctrl+R` (NetBeans), `Alt+Shift+R` (Eclipse), `⇧ F6` (Eclipse (macOS)) 操作时，IntelliJ IDEA 会在高亮显示的元素旁显示 ![注释和字符串中的重命名图标](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.expui.codeInsight.inlayRenameInComments.svg) 和 ![文本匹配项中的重命名图标](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app-client.expui.codeInsight.inlayRenameInNoCodeFiles.svg)。

![在方法名称上调用的重命名操作](https://resources.jetbrains.com.cn/help/img/idea/2026.3/rename_var.png)

您可以按 `Ctrl+Alt+Shift+N` (Windows), `⌘ ⌥ O` (macOS), `⌘ ⌥ ⇧ N` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ O` (macOS System Shortcuts), `Ctrl+Alt+Shift+N` (XWin), `Ctrl+Alt+Shift+N` (GNOME), `Ctrl+Alt+Shift+N` (KDE), `Ctrl+Alt+Shift+N` (Emacs), `Ctrl+Shift+R` (Sublime Text), `⌘ ⇧ R` (Sublime Text (macOS)), `Ctrl+Alt+Shift+T` (Visual Studio), `⌘ ⌥ ⇧ T` (Visual Studio (macOS)), `Ctrl+Alt+Shift+O` (NetBeans), `Ctrl+Alt+Shift+N` (Eclipse), `⌘ ⌥ O` (Eclipse (macOS)) 打开上下文菜单并选择其他重命名选项。

![其他重命名选项](https://resources.jetbrains.com.cn/help/img/idea/2026.3/additional_rename_options.png)

如果您想查看带有更多选项的   重命名  对话框，请点击   更多选项  链接或按 `Shift+F6` (Windows), `⇧ F6` (macOS), `⇧ F6` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ R` (macOS System Shortcuts), `Shift+F6` (XWin), `Shift+F6` (GNOME), `Shift+F6` (KDE), `Shift+F6` (Emacs), `Shift+F6` (Sublime Text), `⇧ F6` (Sublime Text (macOS)), `Ctrl+R, R` (Visual Studio), `⌘ R, R` (Visual Studio (macOS)), `Ctrl+R` (NetBeans), `Alt+Shift+R` (Eclipse), `⇧ F6` (Eclipse (macOS))。

3. 打开（ `Shift+F6` (Windows), `⇧ F6` (macOS), `⇧ F6` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ R` (macOS System Shortcuts), `Shift+F6` (XWin), `Shift+F6` (GNOME), `Shift+F6` (KDE), `Shift+F6` (Emacs), `Shift+F6` (Sublime Text), `⇧ F6` (Sublime Text (macOS)), `Ctrl+R, R` (Visual Studio), `⌘ R, R` (Visual Studio (macOS)), `Ctrl+R` (NetBeans), `Alt+Shift+R` (Eclipse), `⇧ F6` (Eclipse (macOS))） [重命名](rename-dialogs.html)对话框。 输入一个新名称以启用   预览  和   重构  按钮。

![重命名对话框](https://resources.jetbrains.com.cn/help/img/idea/2026.3/rename_dialog.png)

您可以指定其他选项。 例如，指定在哪里搜索元素的出现，或要重命名什么。 您还可以指定重构的范围。

4. 点击   预览  查看潜在的更改或点击   重构  。

当您点击   预览   时，IntelliJ IDEA 会打开 [查找](find-tool-window.html)工具窗口，显示找到的用法，您可以在其中检查结果并确认重构(   执行重构  )。

下次您调用   重命名  重构时，IntelliJ IDEA 会记住您在   重命名  对话框中指定的选项。

> **Note:**
> 无法重命名  `.idea`  项目目录，因为 IntelliJ IDEA 始终从具有该确切名称的目录读取项目文件。

## 示例

让我们重命名一个类  `MyClass` 。

|  之前  |  之后  |
| --- | --- |
|     ```JAVA public class MyClass {     // some code here }      public void myMethod() {         MyClass myClass = new MyClass();     }  ```    |     ```JAVA public class YourClass {     // some code here }      public void myMethod() {         YourClass yourClass = new YourClass();     }  ```    |

> **Tip:**
> 如果您需要编辑变量而不是完全更改它们，请转到  `设置 | 编辑器 | 代码编辑` 并清除位于   重构  部分的   为重命名重构预先选择当前符号名称  复选框。

## See also

### 参考

[重命名对话框](rename-dialogs.html) [查找工具窗口](find-tool-window.html)

