77

Is there a way to organize C# usings (remove and sort, in separate or together) via a shortcut in Visual Studio for one or more files of a project?

I know that this can be done via the menu for one file by selecting

Edit > IntelliSense > Organize Usings > Remove and Sort

but I want to do this much faster. I am using Visual Studio 2013 Express for C# development (wondering how this can be done in older & other versions too though). Thank you in advance.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54

3 Answers3

103

Since VS2017, it's a builtin shortcut. Simply press Ctrl + R, Ctrl + G. Credit goes to Emanuel Ve, who mentioned this first in the comments; I'm putting the advice into an answer for greater visibility.

James Ko
  • 32,215
  • 30
  • 128
  • 239
  • Thank you! Is there an entry in the menus somewhere too? – pratnala Jul 12 '17 at 05:18
  • How do I apply this to all C# code files, solution wide ? – Vinigas Jul 07 '20 at 08:42
  • Can I configure this somehow on how to sort? It is alphabetically but stylecop sometimes brings SA-1208 https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1208.md with some usings that I have. So I will manually move the System.* to the front and would love to have that automatically done by this command. – bugybunny Jul 29 '21 at 12:32
  • Doesn't work in .razor files – derekbaker783 Jun 29 '22 at 20:25
52

Go to Tools => Options => Environment => Keyboard

enter image description here

Enter the key combination you want to use (click the Press shortcut keys: textbox, press your key combo as you would execute it while editing)

enter image description here

Then type "usings" in the Show commands containing: textbox

enter image description here

And now you can assign whichever version of these remove/sort usings commands that you like to the key combination.

This works for pretty much every command in Visual Studio, and from any addons you have installed. Just type a word involved in the command name to find the command. Assign away!

Bob Horn
  • 33,387
  • 34
  • 113
  • 219
  • 2
    If you have Ctrl + F assigned to Edit.Find like the default Visual Studio shortcut, assigning Ctrl + F, Ctrl + U to a using action will remove the find shortcut. If you face this problem, just pick another combination of keys that are not already used as a single key action. – Tot Zam Jul 11 '16 at 14:14
  • 15
    As of Visual studio 2017, I can't find any command containing `using`. However, two commands exist: `Edit.RemoveAndSort` and `EditorContextMenus.CodeWindow.RemoveAndSort` which both clean up the usings. The latter has already a keyboard shortcut which is `Ctrl + R, G` for me, with the default keyboard mapping. – cube45 Apr 24 '17 at 08:25
  • Odd they would deprecate commands like that, but thanks for the info. –  Apr 24 '17 at 12:53
6

In addition to @Will's answer, Productivity Power Tools is a great addon that has all this - and lots lots more!

enter image description here

Pingi
  • 342
  • 3
  • 11
  • 1
    The question is for a shortcut... how can the context menus above be made to appear? Thanks. – track0 Oct 25 '16 at 10:24
  • @track0 when you install the addon detailed in the above answer you can right click the project or a file in it to have it appear. i bet you can also create a short cut for it, but haven't tried it yet. – Pingi Dec 14 '16 at 01:46
  • Productivity Power Tools no longer work since VS 2019 16.9.4. – rustyx May 07 '21 at 20:49