Leonid
Leonid

Reputation: 23450

How to remove unused imports in Intellij IDEA on commit?

Is there a way to remove unused imports in Intellij IDEA on commit?

It's not very optimal to do it manually, CTRL + ALT + O helps, but it's still manual.

Upvotes: 205

Views: 269776

Answers (12)

Josue Abarca
Josue Abarca

Reputation: 343

Now you also have the option to just search for unused imports

  • Code -> Analyze Code -> Run Inspection by Name.
  • Unused Import
  • Whole Project

Source: How to use IntelliJ IDEA to find all unused code?

Upvotes: 3

If you want to use CTRL + Alt + L (remove unused imports while formatting code), you can use CTRL + Alt + Shift + L (once) and there is a checkbox in the popup you should check

enter image description here

Upvotes: 7

Bathri Nathan
Bathri Nathan

Reputation: 1267

To remove the unused imports in windows. please use the default key combination below.

CTRL + ALT + O ---> to remove the unused imports in windows.

However, you can also change the keymap of "Optimize Imports" in settings.

Upvotes: 12

Jay
Jay

Reputation: 1062

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

Upvotes: 51

Steve Pitchers
Steve Pitchers

Reputation: 7364

File/Settings/Inpsections/Imports and change "Unused import" to Error. This marks them more clearly in the Inspections gutter and the Inspection Results panel.

Upvotes: 3

In mac book

IntelliJ

Control + Option + o (not a zero, letter "o")

Upvotes: 15

Damiii
Damiii

Reputation: 1373

Or you can do the following shortcut :

MAC : Shift + Command + A (Enter Action menu pops up)

And write : Optimize Imports

Upvotes: 8

Shravan Ramamurthy
Shravan Ramamurthy

Reputation: 4096

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

Upvotes: 2

Madan Sapkota
Madan Sapkota

Reputation: 26071

If you are using IntelliJ IDEA or Android Studio:

Android optimize imports

Go to Settings > Editor > General >Auto Import and check the Optimize imports on the fly checkbox.

Upvotes: 171

Artur Owczarek
Artur Owczarek

Reputation: 1167

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.

Upvotes: 48

Roman C
Roman C

Reputation: 1

You can check checkbox in the commit dialog.

enter image description here

You can use settings to automatically optimize imports since 11.1 and above.

enter image description here

Upvotes: 45

Peter Lawrey
Peter Lawrey

Reputation: 533432

When you commit, tick the Optimize imports option on the right. This will become the default until you change it.

I prefer using the Reformat code option as well.

Upvotes: 119

Related Questions