How to automatic format code in Visual Studio

daowdos 296 Reputation points
2021-09-13T16:20:33.473+00:00

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.

Developer technologies VB
Developer technologies Visual Studio Other
Developer technologies C#
{count} votes

6 answers

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2021-09-13T17:17:38.393+00:00

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

    131687-format.jpg

    6 people found this answer helpful.

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-09-13T17:35:30.78+00:00

    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.

    5 people found this answer helpful.

  3. PengGe-MSFT 3,381 Reputation points
    2021-09-14T05:32:37.403+00:00

    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

    4 people found this answer helpful.
    0 comments No comments

  4. P a u l 10,761 Reputation points
    2021-09-13T17:18:37.65+00:00

    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

    1 person found this answer helpful.

  5. AgaveJoe 30,126 Reputation points
    2021-09-13T17:52:42.75+00:00

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

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.