Create Shortcut to remove and/or sort Unused Using Statements (Namespaces)

There may be some unused namespaces in your class file and to maintain coding standards we should remove unused namespaces and sort the namespaces.

These are three options to organize usings:

  • Remove Unused Using Statements
  • Sort Unused Using Statements
  • Remove and Sort Unused Using Statements

We can also achieve it by using third party tools. But here we will see how to organize usings by visual studio itself.

Context Menu

You can organize usings by context menu.

Right Click in Editor -> Organize Usings

Create Shortcut

It will be very handy while coding if we will create shortcut for organizing usings. For creating shortcuts:

Tools -> Options -> Keyboard

Search for "Using" in "Show commands containing" textbox:

Here you will see 3 different options for organizing usings:

EditorContextMenu.CodeWindow.OrganizeUsings.RemoveAndSortRemove unused usings and sort remaining usings
EditorContextMenu.CodeWindow.OrganizeUsings.RemoveUnusedUsingsRemove unused usings
EditorContextMenu.CodeWindow.OrganizeUsings.SortUsingsSort usings
  • Select anyone of them. Let's select RemoveUnusedUsings.
  • Specify a shortcut key in "Press shortcut keys" field. Whenever you will press a key, it will tell you whether this shortcut key is used anywhere in visual studio or not by "Shortcut currently used by" field. So use a shortcut key which is not used in VS. Let's use Alt + U + U
  • Click on "Assign" button.
  • Click on "OK" button.

Now your shortcut key is ready to use. You can try it in your class file. Press Alt + U + U and unused usings will be removed and remaining will be sorted.

Hope it will save your time!