51

In Visual Studio 2019 how can I remove unused usings on format document?

I have found instructions for previous versions of Visual Studio (Go to Tools > Options > Text Editor > C# > Code Style > Formatting.). I don't see that in Visual Studio 2019.

Anthony
  • 1,667
  • 2
  • 17
  • 28
  • 3
    In VS2017 it is labelled "Experiment" which generally means it may not survive... (I usually just use Crtl-R,Ctrl-G which is mapped to the context menu comment to sort & remove usings). – Richard Apr 10 '19 at 14:34
  • 1
    You can use using organization on save [like in this post](https://stackoverflow.com/a/55905851/6725736) – Reven Jan 09 '20 at 14:02

9 Answers9

63

In Visual Studio 2019 you can remove unused usings with the Code Cleanup command:

enter image description here

enter image description here

And running code cleanup also formats the document.

Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66
  • 1
    What have happened to the CTRL + K + D shortcut that would this also? – Daniel Lobo May 29 '19 at 15:18
  • 2
    @DanielLobo Ctrl + K, Ctrl + D still formats document. – Sergey Vlasov May 29 '19 at 15:48
  • 1
    @DanielLobo that is where .NET Framework got it wrong. Moving this to a different command abides by the Single Responsibility Principle. – Tom McDonough Jun 08 '19 at 10:51
  • 3
    I'm with OP on this one - I really enjoyed having a single command to do both. It seems like the code cleanup should be configurable to also format the document. Given that code cleanup is already configurable, that seems like it would have hit both camps. – Jason Jul 01 '19 at 15:42
  • It didn't when I tried before but maybe I'd jacked something up. I'll give it another go. [edited to add] - it looks like the thing I was expecting it to do it just doesn't do generally, so that's on me. – Jason Jul 02 '19 at 19:05
44

CTRL + K + E Will sort and remove your unused usings.

Jan
  • 1,661
  • 1
  • 14
  • 7
15

First : go Extensions - > install "Productivity Power tools" then "Power Commands for Visual Studio" extentsion

Second : go tools menu -> Options -> Productivity Power tools -> PowerCommands -> General -> checked "Remove and Sort Usings on save"

note : These options are available only when you use "Power Commands for Visual Studio".

M.Salehi
  • 151
  • 1
  • 5
  • 1
    These options are available only when you use "Power Commands for Visual Studio". Please, mention this in your answer. – kyrylomyr May 04 '20 at 09:27
  • @KyryloM Thanks. I reformatted my computer and couldn't figure out why this had stopped working. I didn't realize this wasn't a part of Power Tools. So glad I saw your comment! – pbristow Jun 11 '20 at 13:30
  • I am using VS 2022, I installed Productivity Power tools, but didn't find "Remove and Sort Usings on save" neither "Power Commands for Visual Studio" – Sharif Yazdian Feb 02 '22 at 20:46
7

The keyboard command that works for me is Edit.RemoveAndSort

Marcello B.
  • 4,177
  • 11
  • 45
  • 65
Garrett
  • 81
  • 1
  • 3
  • Hi @SuitBoyApps, I tried to include a picture but StackOverflow wouldn't let me embed it due to not having enough reputation. The submission form even forced me to use and then replaced my embedded image with the link provided. So I'm at a bit of a chicken and egg problem here. I'm sure it's been a while since you were posting under an account that had no reputation so I'm sure you wouldn't have known this. I'll leave it as is, thanks. – Garrett Jul 29 '20 at 23:31
  • I did not realize it was a picture. I went ahead and added it for you. – Marcello B. Jul 29 '20 at 23:32
5

I always use the combination CTRL + R, CTRL + G. This sorts the usings and removes the unused ones.

Lemraj
  • 279
  • 3
  • 4
5

We can remove it by clicking on the light bulb and selecting" Remove Unnecessary Usings". In addition to removing it from the current document, if we closely look at the bottom of the popup, it can be done for the Project or Solution as well. No need to install or configure anything. This feature is available on VS2015, VS2017, VS2019 and VS2022 on my machine.

enter image description here

Dush
  • 1,185
  • 26
  • 29
3

For single page :

Ctrl+K+E

For all projects:

  1. Right click to your solution on VS.

  2. Click on Analyze and Code Cleanup followed by Run Code Cleanup (Profile 1) enter image description here

  3. And all done, it cleans and formats all over the project.

  4. If you need some more setting on code cleanup , click on configure code cleanup below cleanup profile.

  5. And then add necessary settings. enter image description here

anil shrestha
  • 2,136
  • 2
  • 12
  • 26
0

You can assign both functions to Ctrl + K, Ctrl + D by changing hotkeys and keyboard shortcuts

wale A
  • 81
  • 1
  • 7
0

There is no need to call it by hand, you can use 'cleanup on save' see this post

Reven
  • 776
  • 2
  • 8
  • 23