Is there a way to remove unused imports in Intellij IDEA on commit?
It is not very optimal to do it manually, CTRL + ALT + O helps but it's still manual.
This question is related to
java
import
automation
intellij-idea
In mac book
IntelliJ
Control + Option + o (not a zero, letter "o")
In Mac IntelliJ IDEA, the command is Cmd + Option + O
For some older versions it is apparently Ctrl + Option + O.
(Letter O not Zero 0) on the latest version 2019.x
Or you can do the following shortcut :
MAC : Shift + Command + A (Enter Action menu pops up)
And write : Optimize Imports
You can check checkbox in the commit dialog.
You can use settings to automatically optimize imports since 11.1 and above.
File/Settings/Inpsections/Imports
and change "Unused import" to Error. This marks them more clearly in the Inspections gutter and the Inspection Results panel.
Choose the project/module you want to remove unused import from in Project view. Then from Code menu choose Optimize imports and confirm with Run. The imports will be cleaned in whole project/module.
In IntelliJ, select the project you want to optimize imports on, go to Code menu and choose Optimize imports and a small Optimize Imports popup window will appear. On the popup window you need to click on Run button. Or alternatively, on IntelliJ on Mac, you can use a keyboard short cut Option + Command + O
If you are using IntelliJ IDEA or Android Studio:
Go to Settings > Editor > General >Auto Import and check the Optimize imports on the fly checkbox.
Source: Stackoverflow.com