Organise imports for all files in project
Asked Answered
P

2

36

VSCode includes a setting for organising imports, either via the action menu or automatically on every file save. Executing this command removes any unused imports and reorders imports alphabetically and grouped by package imports followed by local/relative imports.

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

Tutorial here.

Is there any way to apply this to all files in a project without opening and saving each file separately?

Peters answered 3/5, 2019 at 13:6 Comment(0)
E
52

set

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

and run search and replace "import"

enter image description here

Engaging answered 26/12, 2022 at 10:13 Comment(4)
This got me on the right track, but note that it doesn't work to replace text with itself; you have to replace it with something different. Probably the best solution is to replace "import " with "import " (one space into two spaces)... though this could be a problem if you have the word "import" in a string literal somewhere.Kibitka
Then you can turn on regular expressions (.* icon) and add ^ character in front - the beggining of stringDorelle
.........lol :DOdin
Be careful, when using this in large projects, it might take some timeSphere
R
26

I think there is an extension that resembles what you're looking for

https://marketplace.visualstudio.com/items?itemName=bierner.folder-source-actions

You just have to organize the imports from the root folder as explained on the extension page.

Racemic answered 3/5, 2019 at 13:15 Comment(5)
This extension is painful to use. Doesn't impact all the files.Amy
Not sure how right clicking a folder and selecting Organize Imports in folder could be considered painful, but this extension works perfectly for me. Thanks @RacemicFlagellant
What files it doesn't impact?Parathyroid
@LucasSteffen It changes Js and Ts files onlyRacemic
It helps but it misses some files. Thanks anywayUnknow

© 2022 - 2024 — McMap. All rights reserved.