How to automatic format code in Visual Studio

daowdos 261 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.

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,594 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,228 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,569 questions
{count} votes

6 answers

Sort by: Most helpful
  1. Castorix31 81,636 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

    4 people found this answer helpful.

  2. Karen Payne MVP 35,031 Reputation points
    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.

    4 people found this answer helpful.

  3. PengGe-MSFT 3,331 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

    2 people found this answer helpful.
    0 comments No comments

  4. P a u l 10,406 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 26,191 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