DEV Community

Tony Hicks
Tony Hicks

Posted on • Edited on

Automatically organize imports

While refactoring your javascript/typescript code it's easy to remove things but forget to get rid of the unused imports.

Alt Text

In VSCode, you can easily remove these with the shortcut : Shift + Alt + O.
This is sometimes easy to forget to do though, and wouldn't it be better if there was some way to do this automatically every time you save?

In VSCode, go to File -> Preferences -> Settings and click on the icon in the top right hand corner to open up the settings in JSON.

Alt Text

Add the following json:

"editor.codeActionsOnSave": {
    "source.organizeImports": true
},
Enter fullscreen mode Exit fullscreen mode

So that it looks something like the following:
Alt Text

Et voilà!
Your imports will now be organized every time you save a file.
Enjoy :-)

Top comments (3)

Collapse
 
lioness100 profile image
Lioness100

Could you elaborate on how the imports are organized? Or does it just delete unused imports?

Collapse
 
tonyhicks20 profile image
Tony Hicks • Edited

It's the default behavior in VS code - the imports are alphabetized as well.

Collapse
 
lioness100 profile image
Lioness100

Ah, cool!