question

Elado avatar image
0 Votes"
Elado asked PengGe-MSFT commented

How to automatic format code in Visual Studio

I remember I had an automatic code formating right after I pressed ctrl+s.
Now I don't have that.

All I have is "Code Formatter" that makes a lot of commets which I don't need and when I remove those comments with "Clear Comments" it will remove my personal commets also.

131665-no-auto-format-vs.png

Will be super glad for help here.

dotnet-csharpvs-generaldotnet-visual-basic
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, @Elado
I have not heard from you for a couple of days. Please let me know if there is anything that I can help here.

1 Vote 1 ·

Hi, I did ctrl+k, ctrl +d and that did help me.
I’m searching for one time two keys press, its more easy, fast and not confusing!
Thanks

0 Votes 0 ·

Hi, @Elado
I updated my answer please check it

0 Votes 0 ·
Castorix31 avatar image
1 Vote"
Castorix31 answered Castorix31 commented

I have
Ctrl + K, Ctrl + F
to format the selected text
or
Ctrl+ K, + Ctrl + D for the document :

131687-format.jpg



format.jpg (135.1 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you. I will try that with the menu picture you put.
Although I remember Ctrl k or ctrl d didn't do nothing.

0 Votes 0 ·

It can be changed, but Ctrl K on a selection, then Ctrl F, correctly formats the code for me

0 Votes 0 ·
Paul-5034 avatar image
1 Vote"
Paul-5034 answered Elado commented

Here's the extension I use to format documents on save:
https://marketplace.visualstudio.com/items?itemName=MadsKristensen.CodeCleanupOnSave

You should have a default Code Cleanup profile if you go to "Analyze > Code Cleanup > Configure Code Cleanup"

If you move "Format document" from "Available fixers" to "Included fixer" (if it's not already) then press "Ok" and it should format your documents based on your preferences.

There are a few fixers I use that are quite handy (Sort usings/Remove unnecessary usings probably the most useful to me):
131656-image.png



image.png (28.5 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much I’ll try that.

0 Votes 0 ·
karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered Elado edited

You may of used Format Document on Save Visual Studio extension.

Enables auto formatting of the code when you save a file. Visual Studio supports auto formatting of the code with the CTRL+E,D or CTRL+E,F key shortcuts but with this extension the command 'Format Document' is executed on Save.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks Karen !

0 Votes 0 ·
AgaveJoe avatar image
1 Vote"
AgaveJoe answered

The Code Formatter is a 3rd party Visual Studio plugin. Read the openly published documentation to see if this feature is available?


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PengGe-MSFT avatar image
0 Votes"
PengGe-MSFT answered PengGe-MSFT edited

Hi, @Elado

Welcome to Microsoft Q&A!

To format a selection: Ctrl+K, Ctrl+F

To format a document: Ctrl+K, Ctrl+D

See the pre-defined keyboard shortcuts. (These two are Edit.FormatSelection and Edit.FormatDocument.)


Update:
You can Identify and customize keyboard shortcuts in Visual Studio.

Sincerely,
Peng


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.