IntelliJ Organize Imports
Does IntelliJ have an Organize Imports feature similar to that in Eclipse? What I have is a Java file with multiple classes missing their imports. Example:
package com.test;
public class Foo {
public Map map;
public JTable table;
}
In Eclipse I could use Organize Imports and it would automatically import both java.util.Map
and javax.swing.JTable
. In IntelliJ I have to individually go to each class, select it, then press Alt-Enter. There is an Optimize Imports feature but all it seems to do is sort and expand the existing imports.
I'm using IntelliJ 10.5.
Ctrl + Alt + O (Code → Optimize Imports...) is what you're looking for, both on Windows/Linux and macOS keymaps.
It says "Optimize", but, if configured to do so, it will also:
- organize existing imports
- remove unneeded imports
- add new required imports
- add unambiguous imports on the fly
You can tune the auto-import settings under "Settings → Editor → General → Auto Import" as described by Dave.
You can also modify how the imports are auto-ordered under "Settings → Editor → Code Style → Java → Imports"
Under "Settings -> Editor -> General -> Auto Import"
there are several options regarding automatic imports. Only unambiguous imports may be added automatically; this is one of the options.