score:117

Accepted answer

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:

enter image description here

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.

enter image description here

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.

score:-1

use control+option+o to auto-import the package or auto remove unused packages on macos

score:-1

hover on top of the code which needs a class then press

alt + shift + enter

this will auto import the needed class.

score:0

use control+option+l to auto import the package and auto remove unused packages on mac

score:0

use alt+enter for importing a single package or use alt+shift+enter to auto import all the unambiguous packages in the current file.

score:4

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.

enter image description here

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

enter image description here

3- now press ctrl + shift + o. you are done!

score:10

seems like intellij idea will import missed class automatically, and you can import them by hit alt + enter manually.

score:14

can't import all at once but can use following combination:

alt + enter --> show intention actions and quick-fixes.

f2 --> next highlighted error.

score:23

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.

score:79

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.


Related Query

More Query from same tag