Rename refactorings
Shortcut: ShiftF6
Main menu: Refactor | Rename
Context menu: Rename
General refactoring settings: Settings | Editor | Code Editing
In-place refactoring is available
IntelliJ IDEA supports this refactoring for Java 14 code
Use the Rename refactoring to change names of symbols, files, directories, packages, modules and all the references to them throughout code.
Renaming local variables or private methods can be done easily inline since only the limited scope is affected. Renaming classes or public methods could potentially impact a lot of files. Preview potential changes before you refactor.
tip
When you rename methods that are declared in interfaces, you can also review and rename their implementations as well. The refactoring changes are displayed on the Refactoring Preview tab in the Find tool window.
note
The rename process using a gutter icon does not apply to Java files. Renaming in Java files is implemented with an inlay hint.
In the editor start renaming a parameter, a method, or other code element. IntelliJ IDEA will display in the gutter.
Click the gutter icon or press AltEnter and apply a suggestion.
IntelliJ IDEA renames the code element and updates its usages accordingly.
note
The inlay hint rename refactoring applies only to Java files. Other files may use a gutter icon.
In the editor, find the code element you want to rename.
Edit the text of the code element you want to rename, IntelliJ IDEA will display the Rename usages inlay hint.
Select the inlay hint. If IntelliJ IDEA determines that some variables might also be renamed, the Rename Variables dialog opens. To confirm the rename refactoring, select OK.
If IntelliJ IDEA doesn't identify any variables that might also be renamed, IntelliJ IDEA immediately renames the code element and updates its usages accordingly.
In the Project tool window right-click a directory or a module that you want to rename.
From the context menu, select Rename (ShiftF6).
In the popup that opens, select what you want to rename. If you have a module with one content root, and it has the same name as your module, you can rename them both simultaneously.
In the Rename dialog that opens, type the new name, specify additional options and the scope of the refactoring, and click Refactor.
note
If you rename both module and directory at once, in the Rename dialog add the new name, check where to search for references and then click OK.
In the Project tool window, click Project and select Packages from the list. IntelliJ IDEA lists all the packages in your project.
Right-click the package you want to rename and from the context menu, select Rename (ShiftF6).
If you have multiple directories that correspond to the package, or the package contains directories in the libraries that cannot be renamed, IntelliJ IDEA will display a warning. However, you can proceed with one of the offered options in the warning. IntelliJ IDEA opens the Rename dialog where you can enter the new package name and click Preview to see what directories and files will be affected before you decide to refactor. If you want to undo the changes you have made, press Ctrl0Z.
In the editor, select the element you want to rename. If you need to rename a file, select one in the Project tool window.
Press ShiftF6 or right-click and select Rename from the context menu.
When you invoke the rename ShiftF6 action, IntelliJ IDEA displays and next to the highlighted element.
You can press CtrlAltShift0N to open the context menu and select the additional rename options.
If you want to see the Rename dialog with more options, click the More options link or press ShiftF6.
Open (ShiftF6) the Rename dialog. Enter a new name of the element to enable the Preview and Refactor buttons.
You can specify additional options. For example, specify where to search for element occurrences, or what else to rename. You can also specify a scope for the refactoring.
Click Preview to see the potential changes or click Refactor.
When you click Preview, IntelliJ IDEA opens the Find tool window with the results of found usages where you can check the results and confirm the refactoring (Do Refactor).
Next time you invoke the Rename refactoring, IntelliJ IDEA remembers the options you have specified inside the Rename dialog.
note
You cannot rename the .idea project directory since IntelliJ IDEA always reads the project files from the directory with that exact name.
Let's rename a class MyClass
.
Before | After |
---|---|
|
|
tip
If you need to edit variables rather than to change them completely, go to Settings | Editor | Code Editing and clear the Preselect current symbol name for Rename refactoring checkbox located in the Refactorings section.
Thanks for your feedback!