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?
Go to Tools > Options > Text Editor > C# > Code Style > Formatting. It has an experimental Format Document Settings where you can have Visual Studio clean up your code. Two of the options are to "Remove unnecessary usings" and "Sort usings". Check these two and you're good to go!
CTRL + K + E Will sort and remove your unused usings.
From Tools > Option, navigate to Keyboard tab. Search for “Edit. RemoveAndSort” which command will to both remove unused usings and sort the usings, then set shortcut for the same, as I did with “Ctrl + Shift + W” , Click on “Assign” and Ok. you are done.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With