Miguel Moura
Miguel Moura

Reputation: 39374

Sort and Remove unused usings in C# code

Is there a way to Sort and Remove Unused usings in C# just like in Visual Studio 2015 and 2017?

If not, could this be an addition to a future Visual Studio Code version?

Upvotes: 33

Views: 23508

Answers (2)

Daniel Knowlton
Daniel Knowlton

Reputation: 171

There's a couple of extensions available now in Visual Studio Code that can help with this:

Name: C# Sort Usings
Id: jongrant.csharpsortusings
Description: Sort using statements
Version: 0.0.6
Publisher: Jon Grant
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=jongrant.csharpsortusings\

Name: C# Format Usings
Id: gaoshan0621.csharp-format-usings
Description: Sort C# using statements and remove unnecessary usings
Version: 0.0.4
Publisher: Shan Gao
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=gaoshan0621.csharp-format-usings\

Upvotes: 4

alexriedl
alexriedl

Reputation: 6463

There is a command that is built into vscode to help out removing unused usings.

The default keybinding is defined as:

{ "key": "ctrl+.", "command": "editor.action.quickFix",
                      "when": "editorHasCodeActionsProvider && 
                               editorTextFocus && 
                               !editorReadonly" },

As for sorting the usings: I did not find anything built explicitly for using statements, but there is a command to sort lines. Add something similar to the following into your keybindings file:

{ "key": "ctrl+q", "command": "editor.action.sortLinesAscending",
                      "when": "editorFocus && !editorReadonly" },

Then, select the using statements and press ctrl+q to sort them.

Upvotes: 35

Related Questions