Auto import
tip
This page describes Java imports. For more information about imports in Kotlin, refer to Packages and Imports.
If you're using a class, a static method, or a static field that you haven't imported yet, the IDE shows you a tooltip prompting you to add a missing import statement so that you don't have to add it manually. Press AltEnter to accept the suggestion.
If there's more than one possible source of import, pressing AltEnter will open the list of suggestions.
To change the background color for the import tooltip, press CtrlAlt0S and go to Editor | Color Scheme | General | Popups and Hints | Question hint.
tip
Statically imported methods, classes, fields have different style in the editor.
You can configure the IDE to automatically add import statements if there are no options to choose from.
Press CtrlAlt0S to open settings and then select Editor | General | Auto Import.
Select the Add unambiguous imports on the fly checkbox, and apply the changes.
When you are pasting blocks of code that contain references to classes or static methods and fields that are not yet imported, the IDE automatically inserts the missing import statements. If you want to change that, from the Insert imports on paste list, select Ask to confirm every insertion or Never to insert import statements manually.
IntelliJ IDEA suggests importing single classes by default. You can change the settings to import entire packages instead.
Press CtrlAlt0S to open settings and then select Editor | Code Style | Java | Imports.
Clear the Use single class import checkbox, and apply the changes.
When tooltips are disabled, unresolved references are underlined and marked with the red bulb icon . To view the list of suggestions, click this icon (or press AltEnter) and select Import class.
Hover over the inspection widget in the top-right corner of the editor, click , and disable the Show Auto-Import Tooltip option.
Press CtrlAlt0S to open settings and then select Editor | General | Auto Import.
Make sure the necessary options are selected for the Show auto-import tooltip for setting (both checkboxes, Classes and Static methods and fields, are enabled by default).
When the number of classes that IntelliJ IDEA has imported from the same package reaches the limit (5
by default), the IDE modifies the statements to import the entire package instead of importing several single classes from this package:
Press CtrlAlt0S to open settings and then select Editor | Code Style | Java.
Make sure that the Use single class import option is enabled.
In the Class count to use import with '*' and Names count to use static import with '*' fields, specify values that definitely exceed the number of classes in a package and the number of names in a class (for example,
999
).
To replace an import statement with single class imports in a file without changing the settings, place the caret at the import statement, press AltEnter (or use the intention action icon), and select Replace with single class imports.
If you want to completely disable auto-import, make sure that:
The automatic insertion of import statements is disabled.
The list of import suggestions may include classes and packages that you don't need. You can exclude redundant entries from automatic import so that the list of suggestions contains only relevant items.
tip
The list of excluded classes and packages is common for auto-import and code completion.
Press CtrlAlt0S to open settings and then select Editor | General | Auto Import.
In the Exclude from auto-import and completion section, click AltInsert, and specify a class or a package that you want to exclude.
You can also select whether you want to exclude items from the current project or from all projects (globally).
Press AltEnter on a missing class to open the list of import suggestions.
Click the right arrow next to a package and select an item (a class or an entire package) that you want to exclude.
In the Exclude from auto-import and completion section of the Auto Import dialog, select whether you want to exclude items from the current project or from all projects, and apply the changes.
The Optimize Imports feature helps you remove unused imports and organize import statements in the current file or in all files in a directory at once according to the rules specified in Settings | Editor | Code Style | <language> | Imports.
tip
You can exclude specific files and folders from import optimization. For more information, refer to Exclude files from reformatting.
Select a file or a directory in the Project tool window (View | Tool Windows | Project).
Do any of the following:
In the main menu, go to Code | Optimize Imports (or press CtrlAlt0O).
From the context menu, select Optimize Imports.
(If you've selected a directory) Choose whether you want to optimize imports in all files in the directory, or only in locally modified files (if your project is under version control), and click Run.
Place the caret at the unused import statement and press AltEnter or use the icon.
Unused statements are greyed out by default.
From the list of suggestions, select Remove unused imports.
If your project is under version control, you can configure IntelliJ IDEA to optimize imports in modified files before committing them to VCS.
note
Press Alt00 to open the Commit tool window and click Show Commit Options .
Click and in the commit message area, select the Optimize imports checkbox.
You can configure the IDE to optimize imports in modified files automatically when your changes are saved.
Press CtrlAlt0S to open settings and then select Tools | Actions on Save.
Enable the Optimize imports option.
Additionally, from the All file types list, select the types of files in which you want to optimize imports.
Apply the changes and close the dialog.
You can tell IntelliJ IDEA to optimize imports in a file every time it is reformatted.
Open the file in the editor, press CtrlAltShift0L, and make sure the Optimize imports checkbox is selected in the Reformat File dialog that opens.
After that every time you press CtrlAlt0L in this project, IntelliJ IDEA will optimize its imports automatically.
note
When you create a class from a template with the Optimize imports on the fly option enabled, IntelliJ IDEA will optimize imports from this template only after you reopen the project.
You can also configure the IDE to automatically optimize imports. IntelliJ IDEA will remove or modify import statements according to the rules specified in Settings | Editor | Code Style | <language> | Imports as you work in the editor.
Press CtrlAlt0S to open settings and then select Editor | General | Auto Import.
Enable the Optimize imports on the fly option and apply the changes.
Thanks for your feedback!