34

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?

5
  • 1
    Please note that your second question is off-topic here: this is not a forum for making feature requests to any third-party product. Commented Mar 28, 2017 at 14:41
  • @IMSoP In a way I agree with you but on the other hand even MSFT recommends to ask here: github.com/Microsoft/vscode/issues/new (See the message). I was going to create an Issue. I have seen this not only with Visual Studio Code. Commented Mar 28, 2017 at 14:43
  • What Microsoft says on an external site does not over-ride the policies of this site. See also stackoverflow.com/help/product-support In this case, your first question seems absolutely fine, but if the answer is "no, there's no way in the current version", the feature request to add it will need to be raised as an issue elsewhere. Commented Mar 28, 2017 at 14:47
  • @IMSoP, sure, if there is none then I will create an Issue on Github. That was my initial idea. I just could not find a way anywhere to do this with Visual Studio Code so I wanted to clarify before I create an Issue. Commented Mar 28, 2017 at 14:59
  • Try install Beautify extension. Hover on using keyword and you get an yellow bulb icon and click on that you can see a context menu "remove unused namespaces". Commented Jun 13, 2018 at 1:04

2 Answers 2

37

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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\

1 Comment

Unfortunately neither of these extensions seem to offer a "execute on save" setting.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.