336

Is there some plug-in of Visual Studio Code or other means that could help us to tidy and organize the imports and references quickly and effectively?

For example, there are maybe hundreds of imports like this

import { AutoCompleteModule,InputSwitchModule } from 'primeng/primeng';
import { ListboxModule } from 'primeng/primeng';

could be converted to similarily

import { AutoCompleteModule,
         InputSwitchModule,
         ListboxModule  } from 'primeng/primeng';

Or other functions like automatically removing those unused imports and declarations from the app.module or from all components throughout the project?

Thanks for any feedback!

jgu7man
  • 326
  • 1
  • 3
  • 10
ske
  • 4,694
  • 3
  • 23
  • 35
  • I too am wondering if extraneous imports into components specifically are a drag on performance. – Marcidius Oct 18 '17 at 14:22
  • 33
    https://stackoverflow.com/a/49697144/3914072 vscode 1.22 Shift+Alt+O - works for me! – Rajab Shakirov Apr 08 '18 at 18:22
  • Can be done from the command line (or git hook) too: https://www.npmjs.com/package/organize-imports-cli – thorn0 Aug 12 '19 at 09:30
  • I had this question too before creating an extension for that (https://marketplace.visualstudio.com/items?itemName=kuscamara.remove-unused-imports) and discovering just after publishing it, that it can be done via Code Actions (Ctrl/Cmd + .) over an unused import to remove it or all the unused imports without sorting them. – kcmr Aug 14 '21 at 10:25

6 Answers6

578

Edit (as suggested in comments and other people), Visual Studio Code has evolved and provides this functionality in-built as the command "Organize imports", with the following default keyboard shortcuts:

option+Shift+O for Mac

Alt + Shift + O for Windows


Original answer:

I hope this visual studio code extension will suffice your need: https://marketplace.visualstudio.com/items?itemName=rbbit.typescript-hero

It provides following features:

  • Add imports of your project or libraries to your current file
  • Add an import for the current name under the cursor
  • Add all missing imports of a file with one command
  • Intellisense that suggests symbols and automatically adds the needed imports "Light bulb feature" that fixes code you wrote
  • Sort and organize your imports (sort and remove unused)
  • Code outline view of your open TS / TSX document
  • All the cool stuff for JavaScript as well! (experimental stage though, better description below.)

For Mac: control+option+o

For Win: Ctrl+Alt+o

Stephen Jennings
  • 12,494
  • 5
  • 47
  • 66
Muhammad Rehan Qadri
  • 6,824
  • 1
  • 17
  • 18
  • 2
    Thanks for turning me onto this add-on, it rocks! Up until I just installed it I used to have Auto-Import extension installed to take care of that little piece of functionality (auto importing). But after installing TypeScript Hero... wow, it does everything I need it to do, including sorting dependencies in alphabetical order within the import statements themselves, getting rid of imports that are not used within the component classes, etc! – Marcidius Oct 18 '17 at 14:21
  • 3
    In 2018 TS Hero's project maintainer [has said](https://gitlab.com/smartive-private/christoph/typescript-hero/issues/434#note_107457812) he would discontinue the extension, since it has become obsolete after the implementation of the main functionalities directly in VS Code (see other coments). – mattarau Mar 31 '19 at 21:02
  • 11
    Any way to call `Alt+Shift+O` without reordering the imports? – XCS Mar 15 '20 at 23:40
  • 3
    Alt + Shift + O for Linux too – José Manuel Blasco Jul 07 '20 at 07:49
  • it just displays a popup, where is the automatically part in this answer ? – phil123456 Nov 16 '20 at 09:00
  • 2
    Currently, one of the benefits of TypeScript Hero over VS Code's organizer is being able to separate/group imports (e.g. global and local imports separated by new line). If you want to organize imports on save with this plugin, you can add the following in settings.json `"typescriptHero.imports.organizeOnSave": true` – OJ7 Jun 28 '21 at 18:25
  • Not sure why, but for me, it was `command shift o`. Using mac and vs code version `1.58.0` – Alex Tarasenko Jul 13 '21 at 06:40
  • 3
    +1 to @XCS. Any way possible to use "organize imports" command only for removing unused imports and declarations and not reordering the imports? – Waleed Tariq Oct 08 '21 at 10:29
  • How can I run this for all the typescript/angular files ? – Mauricio Gracia Gutierrez Dec 28 '21 at 15:06
  • This has recently stopped working for me! – david_adler Mar 31 '22 at 21:27
  • works only for javascript/typescript, not for C# – serge Dec 08 '22 at 12:19
211

If you're a heavy visual studio user, you can simply open your preference settings and add the following to your settings.json:

...
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
  "source.organizeImports": true
}
....

Hopefully this can be helpful!

JayKan
  • 3,966
  • 2
  • 20
  • 21
  • 7
    This seems to conflict with ESLint Prettier plugin. It is trying to make an one-line import but ESLint is trying to break in multiple lines. – richardaum Mar 10 '19 at 22:52
  • Same problem as @Richard. Seems to be an open issue - https://github.com/prettier/prettier-vscode/issues/716 – Craig Feb 04 '20 at 10:26
  • 2
    Is there a way to disable the removal of unused imports while keeping imports sorted? – sunknudsen Mar 15 '20 at 13:19
  • In my experience doing auto organise imports and formats shouldn't be done on save as it can cause git conflict nightmares. It should be up to the developer to do it before saving the file if they wish. If they are working on new files it's ok but not when you are just applying hot fixes to existing code. Sure, it's easier to use something like Husky or Github actions on the post-commit stage to lint files but legacy projects normally won't have this and you only want to see change diffs and not formatting diffs. – Brad Bird Apr 15 '21 at 15:40
  • Can I set this for the whole team? – J4N Apr 11 '22 at 15:07
  • Is there something similar for unused variables ? – Pawan Jan 20 '23 at 07:19
193

As of Visual Studio Code Release 1.22 this comes free without the need of an extension.

Shift+Alt+O will take care of you.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Aaron Jordan
  • 2,447
  • 2
  • 15
  • 13
64

To be able to detect unused imports, code or variables, make sure you have this options in tsconfig.json file

"compilerOptions": {
    "noUnusedLocals": true,
    "noUnusedParameters": true
}

have the typescript compiler installed, ifnot install it with:

npm install -g typescript

and the tslint extension installed in Vcode, this worked for me, but after enabling I notice an increase amount of CPU usage, specially on big projects.

I would also recomend using typescript hero extension for organizing your imports.

Matheus Frik
  • 835
  • 7
  • 10
56

Since VSCode v.1.24 and TypeScript v.2.9 Organize Imports was added:

For Mac: option+Shift+O

For Win: Alt+Shift+O

kenny
  • 1,628
  • 20
  • 14
  • 7
    more important to some, the command is `Organize Imports` or `editor.action.organizeImports` – pcnate Aug 23 '18 at 19:56
  • 1
    @pcnate Is there a way to disable the removal of unused imports while keeping imports sorted? – sunknudsen Mar 15 '20 at 13:19
  • 1
    That keybinding doesn't work for me, instead, I run `Shift` + `CMD` + `P` and searched for Organize Imports and It works – jgu7man Jan 14 '23 at 14:18
26

There are already so many good answers on this thread! I am going to post this to help anybody trying to do this automatically! To automatically remove unused imports for the whole project this article was really helpful to me.

In the article the author explains it like this:

Make a stand alone tslint-imports.json file that has the following in it:

{
  "extends": ["tslint-etc"],
  "rules": {
    "no-unused-declaration": true
  }
}

Then run the following command to fix the imports:

 tslint --config tslint-imports.json --fix --project .

Consider fixing any other errors it throws. (I did)

Then check the project works by building it:

ng build

or

ng build name_of_project --configuration=production 

End: If it builds correctly, you have successfully removed imports automatically!

NOTE: This only removes unnecessary imports. It does not provide the other features that VS Code does when using one of the commands previously mentioned.

Chris Fremgen
  • 4,649
  • 1
  • 26
  • 26
Matt Bussing
  • 556
  • 6
  • 10
  • 1
    I get ```Could not find implementations for the following rules specified in the configuration: no-unused-declaration ```, so I guess this solution doesn't work anymore. – Yousuf Khan Apr 21 '20 at 09:37
  • I am using tslint version `5.20.1` – Yousuf Khan Apr 21 '20 at 10:05
  • This helped me; thanks. The one thing that I added for my project (React) was an ignore option to `"no-unused-declaration"`. See [this tslint-imports.json](https://gist.github.com/eriknomitch/a8397b92cff01b8e96acbede4c4113bf) for an ignore example. – Erik Nomitch Apr 05 '21 at 18:47
  • There is an extension just for that (https://marketplace.visualstudio.com/items?itemName=kuscamara.remove-unused-imports). I created it because the native “Organize imports” provided by VS Code also changes the order of the imports, that’s annoying for me. – kcmr Jan 29 '22 at 12:55