What keyboard shortcut is there to organize C# usings in Visual Studio?
Asked Answered
B

4

80

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.

Blockbusting answered 27/1, 2015 at 15:17 Comment(2)
since VS2017, use Ctrl-R + Ctrl-GFertilization
Here is a thread for solutions for VS 2019 https://mcmap.net/q/260806/-in-visual-studio-2019-how-can-i-remove-unused-usings-on-format-document/6300406Banbury
V
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!

Vat answered 27/1, 2015 at 15:28 Comment(3)
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.Burne
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.Astrodome
Odd they would deprecate commands like that, but thanks for the info.Vat
W
110

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.

Wamsley answered 8/7, 2017 at 5:9 Comment(4)
Thank you! Is there an entry in the menus somewhere too?Debouch
How do I apply this to all C# code files, solution wide ?Heinous
Can I configure this somehow on how to sort? It is alphabetically but stylecop sometimes brings SA-1208 github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/… 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.Alleyne
Doesn't work in .razor filesKimber
V
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!

Vat answered 27/1, 2015 at 15:28 Comment(3)
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.Burne
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.Astrodome
Odd they would deprecate commands like that, but thanks for the info.Vat
B
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

Boer answered 23/7, 2015 at 10:11 Comment(3)
The question is for a shortcut... how can the context menus above be made to appear? Thanks.Pudendas
@Pudendas 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.Boer
Productivity Power Tools no longer work since VS 2019 16.9.4.Caramel
B
1

In Visual Studio: ctrl + K + D organizes all the lines

Ben answered 31/1, 2024 at 22:44 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.