Reputation: 2931
In Eclipse, while coding in Java and press Ctrl + Shift + O auto import all the Classes automatically.
In NetBeans, this is done with Ctrl + Shift + I.
Is any way to do this in IntelliJ IDEA?
I searched an equivalent shortcut in google, StackOverflow, IntelliJ IDEA configuration and in the official IntelliJ website Keyboard Shortcuts You Cannot Miss
Upvotes: 168
Views: 264353
Reputation: 1
With the blinking cursor positioned on the line where your "extends Something" is located, just press "ctrl + ." on your keyboard.
This will either automatically import the class you want to extend or open a list where the first option will be the class you need to import. Then, just press Enter to confirm.
So, "ctrl + ." or "ctrl + ." and than "Enter".
@edit
As a beginner in programming, I was looking for a simple solution to a specific problem. Most answers I found, even widely validated ones, didn’t offer anything as straightforward as the two-key shortcut my IDE suggested as a default shortcut (so, there is one). Some suggested reconfiguring the environment, which seemed unnecessary for my issue, or shortcuts that didn't work for me. To help others with a similar need, I’m sharing this simple solution. I hope it meets the community's criteria, and as a newcomer, I welcome all feedback to guide me in the community's guidelines. Thank you!
Upvotes: 0
Reputation: 69
As others mentioned, IntelliJ auto imports them on the fly (if enabled in the settings).
If you want to do this across an entire project, what you can do is replace all (CTRL+SHIFT+H
) occurrences of the class name to include the package too (e.g. MyClass
-> com.app.MyClass
), and then you can run the "Remove Unnecessary Qualifications" inspection on the whole project by pressing ALT+Enter
on some code that was replaced. Then once the inspection is done, just keep clicking the "Replace qualified name with import" button on the right.
I couldn't find any other way to do the project-wide auto import
Upvotes: 0
Reputation: 11607
UPDATE
@ntg has given a very smart solution for converting import .*
to individual imports in existing files (it is not completely automatic, but still quite low-effort)
import .*
statement and press Alt Enter (⌥ return on Mac)
Original answer
Not sure if this is universal but I've found that after enabling both the following settings (as told in earlier answers), my IntelliJ
is able to smartly convert package.*
imports into individual imports (in existing files) just by deleting the import com.company.package.*
line
Essentially it will be able to add all 'unambiguous' imports for us, the rest will have to be resolved manually
I'm using
Upvotes: 1
Reputation: 4275
Another option is to ask IDEA to behave like eclipse with eclipse shortcut keys. You can use all eclipse shortcuts by enabling this.
Here are the steps:
1- With IDEA open, press Control + `. Following options will be popped up.
2- Select Keymap. You will see another pop-up. Select Eclipse there.
If you don't see "Keymap" in the options, install "Eclipse Keymap" plugin
3- Now press Ctrl + Shift + O
. You are done!
Upvotes: 5
Reputation: 140
Hover on top of the code which needs a class then press
alt + shift + Enter
This will auto import the needed class.
Upvotes: -1
Reputation: 1
Use Alt+Enter for importing a single package or use Alt+Shift+Enter to auto import all the unambiguous packages in the current file.
Upvotes: -1
Reputation: 103
Use control
+option
+O
to auto-import the package or auto remove unused packages on MacOS
Upvotes: -2
Reputation: 7708
Use control+option+L to auto import the package and auto remove unused packages on Mac
Upvotes: 0
Reputation: 2711
Can't import all at once but can use following combination:
ALT
+ Enter
--> Show intention actions and quick-fixes.
F2
--> Next highlighted error.
Upvotes: 21
Reputation: 4127
I think the best solution, though not exactly the same as Eclipse/Netbeans, is to change the 'Optimize Imports' settings.
Under Preferences > Editor > General > Auto Import
Set Add unambiguous imports on the fly
Edit: Using this method, when there are ambiguous imports, IntelliJ will let you know, and you can then use Alt + Enter method outlined in the answer by Wuaner
I find that, almost always, the most appropriate Import is at the top of the list.
Upvotes: 35
Reputation: 969
Seems like IntelliJ IDEA will import missed class automatically, and you can import them by hit Alt + Enter manually.
Upvotes: 10
Reputation: 6762
Not all at once. But you can press
Alt + Enter
People assume it only works when you are at the particular item. But it actually works for "next missing type". So if you keep pressing Alt + Enter, IDEA fixes one after another until all are fixed.
Upvotes: 83
Reputation: 31906
IntelliJ IDEA does not have an action to add imports. Rather it has the ability to do such as you type. If you enable the "Add unambiguous imports on the fly" in Settings > Editor > General > Auto Import, IntelliJ IDEA will add them as you type without the need for any shortcuts. You can also add classes and packages to exclude from auto importing to make a class you use heavily, that clashes with other classes of the same name, unambiguous.
For classes that are ambiguous (or is you prefer to have the "Add unambiguous imports on the fly" option turned off), just type the name of the class (just the name is OK, no need to fully qualify). Use code completion and select the particular class you want:
Notice the fully qualified names to the right. When I select the one I want and hit enter, IDEA will automatically add the import statement. This works the same if I was typing the name of a constructor. For static methods, you can even just keep typing the method you want. In the following screenshot, no "StringUtils" class is imported yet.
Alternatively, type the class name and then hit Alt+Enter or ⌥+Enter to "Show intention actions and quick-fixes" and then select the import option.
Although I've never used it, I think the Eclipse Code Formatter third party plug-in will do what you want. It lists "emulates Eclipse's imports optimizing" as a feature. See its instructions for more information. But in the end, I suspect you'll find the built in IDEA features work fine once you get use to their paradigm. In general, IDEA uses a "develop by intentions" concept. So rather than interrupting my development work to add an import statement, I just type the class I want (my intention) and IDEA automatically adds the import statement for the class for me.
Upvotes: 139