76

I'm working with intelliJ and my problem is when I start to import some temporary unused packages into my class file intellij delete those line within a second.

how can I turn off this not so nice feature?

user1338062
  • 11,939
  • 3
  • 73
  • 67
Vahid Hashemi
  • 5,182
  • 10
  • 58
  • 88

6 Answers6

99

Disable File | Settings | Editor | General | Auto Import | Optimize imports on the fly.

Normally you don't need to add imports manually, IDEA does it for you.

klutt
  • 30,332
  • 17
  • 55
  • 95
CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • 23
    While this the common case in Java, with Scala and implicit conversions, things are not as smooth. For example, it keeps removing `import scala.collection.JavaConverters._`. But IntelliJ doesn't auto-import when I type `.asScala` at the end of an expression that returns a Java collection. So IMHO the option should be off by default for Scala code. – Dilum Ranatunga Nov 04 '14 at 19:27
  • 1
    For me, the Auto Import option was under `Settings > Editor > General > Auto Import > Optimize imports on the Fly`, as in @merrick's answer. v14.1.4 – Max Heiber Jul 15 '15 at 15:44
  • 3
    @CrazyCoder I'm using IDEA 15.0.1 Build 143.382 and do not have this enabled. However, IDEA removes my imports whenever I comment out lines. Any idea? – Glide Jan 29 '16 at 19:57
  • how can I disable this for a single Kotlin file? The damn IDE is constantly inserting java `String` and `Set` imports into this one file and it produces error messages of course. Very annoying since it happens whenever imports are optimized for it – ycomp Feb 15 '17 at 01:13
  • Groovy seems to be affected as well. Some imports are marked as unused but they are required for compilation. I have noted this happens specially when using inner classes of an extended class. – lepe Dec 27 '17 at 01:07
  • God forbid you have two packages with the same name!!! This "remove unused imports" feature drives me crazy. Auto add YES. But don't remove a package I already used IntelliJ. BTW, this is missing in IntelliJ Ultimate Golang plugin. – akahunahi Jul 16 '18 at 18:05
17

For Scala developers: you can have enabled

Settings > Editor > General > Auto Import > Optimize imports on the fly

but add exclusions into

Settings > Editor > Code Style > Scala > Imports always marked as used:

enter image description here

nafg
  • 2,424
  • 27
  • 25
Sasha
  • 1,393
  • 16
  • 17
  • 1
    Thank you! I didn't know that and that drove me crazy. Very useful for Scala specially when you start using a lot things like Cats or ScalaZ... – Guillaume Jan 23 '19 at 11:49
12

In Intellij 14 : Settings > Editor > General > Auto Import > Optimize imports on the fly.

merrick
  • 396
  • 3
  • 6
3

(Update) For the Scala developers out there, you can have 1/2 the cake and eat the other 1/2 now: disable the auto-optimize for just scala

enter image description here

Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52
1

For me while using Go, this was caused by optimizing imports on save. IntelliJ IDEA -> Prefernces -> Tools -> Actions on Save -> Optimize imports.

samlandfried
  • 801
  • 9
  • 11
0

The above answers are obviously the way to go. Here's another quick fix (a bad one at that) is to simply use it somewhere in your code

if (0) console.log(LibraryName.version);

This should only be a temporary measure though.

DanielK
  • 792
  • 1
  • 6
  • 13