55

There is a new feature in TypeScript 2.8 that lets you "Organize imports": https://devblogs.microsoft.com/typescript/announcing-typescript-2-8-2/#organize-imports

Basically it does the following:

  • remove unnecessary import statements
  • sort the import statements

The page shows that the feature can be invoked in Visual Studio Code with the Shift+Alt+O keyboard shortcut. Does anybody know how this feature can be invoked in Visual Studio (2017)?

UPDATE:

From version 15.8 onward Visual Studio 2017 highlights unused imports by greying them out.

6 Answers 6

61

Visual Studio Code has released a new feature in April last year which enable to organize imports on save. Can you try to update your current settings.json with the following changes:

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

Hopefully this can be helpful and good luck!

3
  • 1
    is there a way to have it put an empty line between like-blocks? E.g., global imports, absolute path imports, relative imports all separate?
    – MrColes
    Jun 24, 2020 at 1:41
  • Does it work for commonJS imports as well? I mean for packages imported using require keyword. Oct 1, 2021 at 19:05
  • 1
    Beautiful. I also added "source.removeUnusedImports": true. Aug 9 at 21:44
44

You can also use the following shortcut with your keyboard to "Organize imports":

Shift+Alt+O

3
  • Is this for VS or VS Code? Feb 3, 2020 at 18:54
  • @JennyO'Reilly VSC
    – sunknudsen
    Mar 18, 2020 at 16:17
  • 2
    Ok, thanks. The original question was for VS (not Code). Mar 18, 2020 at 18:07
28

A slightly modified version of JayKan's answer but this worked for me in VSCode settings.json.

    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    }
2
  • I think this must be accepted answer. Time saver answer. Jul 19 at 14:13
  • Yes this is working for me too. Although I later switched it off as it was annoying me by deleting unused imports. Generally helpful, but a disaster when you are moving code between files, and thereby losing the relevant imports in the "old" file. Also a bit miserable when you bring a set of imports in from a template file and then try to write code that uses them. The ones you haven't used yet, keep disappearing. Jul 23 at 9:19
19

This feature has now been added to Visual Studio 2017.

One can invoke it the following ways:

  1. Pressing Ctrl+R followed by Ctrl+G.
  2. Right-click in the code window and click Organize Imports in the context menu.
2

In Visual Studio 2022 (might even be in previous releases too) the option Organize Imports in the editor's code context menu has been renamed to Remove and Sort Usings, which still does the same thing and also remains available using the shortcut CTRL+R followed by CTRL+G.

Just in case anyone overlooked it, as I did.

1
  • The shortcut wasn't set, go to preferences -> Keyboard Shortcuts. I set mine to cmd + shift + o Thanks! Aug 4 at 17:28
-1

This is a feature of Visual Studio Code and not Visual Studio 2017.

4
  • The Microsoft site says: "TypeScript’s language service now provides functionality to organize imports." So it should not be a feature of VS Code. Maybe it is not callable in VS 2017 yet. Jul 19, 2018 at 6:59
  • BTW. I use Resharper for Visual Studio. It has Code Cleanup function with import organizing feature for Typescript. However not so good as VS Code Jul 20, 2018 at 8:04
  • VS Code merges and sorts multiple imports from same source into one line. Visual Studio doesn't. VS Code adds new imports as you type more accurately. VIsual Studio sometimes does, somethimes doesn't. Anycase, typescript in Visual Studio vs VS Code is a question of personal taste. Jul 21, 2018 at 16:11
  • From version 15.8 onward Visual Studio 2017 now highlights unused imports by greying them out. Aug 24, 2018 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.