Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organize Imports Automatically

Tags:

java

eclipse

I find myself typing Ctrl+Shift+O several times to organize imports (usually imports which are no longer required, generating warnings).

Is there to make Eclipse do this every once in a while or, at least, every time I save a file?

like image 468
EclipseQuestion Avatar asked Jun 09 '11 13:06

EclipseQuestion


People also ask

How do I turn on auto import?

In the Settings/Preferences dialog ( Ctrl+Alt+S ), click Editor | General | Auto Import. Enable the Optimize imports on the fly option and apply the changes.

How do you organize imports in Python?

Organize imports into groups: first standard library imports, then third-party imports, and finally local application or library imports. Order imports alphabetically within each group. Prefer absolute imports over relative imports. Avoid wildcard imports like from module import * .


4 Answers

Preferences->Java->Editor->Save Actions->Configure...

You can configure the removal of unused imports when saving from the Unnecessary Code tab.

Adding imports would normally be done as you are writing the code otherwise it won't compile.

This is how it looks like: enter image description here

like image 159
Robin Avatar answered Oct 23 '22 21:10

Robin


Doing while saving is great, but if you already have lots of files that need it applied to you can also select the package in the Package Explorer and go to Source->Organise Imports It will apply to each file below that package.

You may be doing this already, I just wanted to make it clear that it can be applied to many files as can Source->Format.

like image 41
weston Avatar answered Oct 23 '22 23:10

weston


Either: <project>->Properties->Java Editor->Save Actions

Or

Windows->Preferences->Java->Editor->Save Actions

like image 22
Puce Avatar answered Oct 23 '22 21:10

Puce


Yes, it is called Save Actions. There you can enable several thing should be done when saving a file.

like image 43
Fabian Barney Avatar answered Oct 23 '22 21:10

Fabian Barney