Вопрос:

How do you auto format code in Visual Studio?

visual-studio indentation

1034221 просмотра

26 ответа

3529 Репутация автора

I know Visual Studio can auto format to make my methods and loops indented properly, but I cannot find the setting.

Автор: user719550 Источник Размещён: 22.04.2011 01:33

Ответы (26)


1137 плюса

15133 Репутация автора

Решение

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.)

Автор: Bogdan Verbenets Размещён: 22.04.2011 01:37

41 плюса

6983 Репутация автора

Follow the steps below:

  • Go to Tools
  • Go to Options
  • Go to the Text Editor options
  • Click the language of your choice. I used C# as an example.

See the below image:

enter image description here

Автор: Pete Размещён: 22.04.2011 01:38

10 плюса

449 Репутация автора

If you display the HTML Source Editing toolbar there is a "Format the Whole Document" button as well

Автор: Starwfanatic Размещён: 22.04.2011 01:47

140 плюса

3677 Репутация автора

For Visual Studio 2010/2013/2015/2017

  • Format Document (Ctrl+K,Ctrl+D) so type Ctrl+K, AND THEN Ctrl+D as it is a sequence
  • Format Selection (Ctrl+K,Ctrl+F)

Toolbar Edit -> Advanced (If you can't see Advanced, select a code file in solution explorer and try again)

Your shortcuts might display differently to mine as I am set up for C# coding but navigating via the toolbar will get you to your ones.

If it isn't working, look for errors in your code, like missing brackets which stop auto format from working

Автор: ono2012 Размещён: 04.10.2012 09:49

7 плюса

2336 Репутация автора

I used to use these combinations. I automated this process on Save of a document. You can try mine extension Format Document on Save

Автор: mynkow Размещён: 24.01.2014 01:15

8 плюса

387 Репутация автора

The solution provided in accepted answer does not apply to Microsoft Visual Studio 2012.

In case of VS2012 the shortcuts are:

  • For a highlighted block of code: Ctrl + K, Ctrl + F
  • For the document-wide formatting: Ctrl + K, Ctrl + D
Автор: Geowil Размещён: 21.03.2014 11:59

5 плюса

339 Репутация автора

Under Under Tools -> Options -> Text Editor, then going to the Formatting -> General section of whatever language you wish to format you will find General. Check all three formatting check-boxes.

Under Tools -> Options -> Text Editor, then going to the TABS section of whatever language you wish to format you will find Indenting. Select Smart and it will activate automatic formatting whenever you use one of the closing elements ; ) } within that block.

No need for keystrokes.

Автор: Eric Размещён: 20.01.2015 10:27

25 плюса

2983 Репутация автора

Even though the question is a bit old, someone might find it handy.

You can define new key bindings by going to Tools -> options -> Environment -> keyboard

enter image description here

Автор: Sami Размещён: 11.03.2015 03:51

4 плюса

191 Репутация автора

You can add the buttons to your toolbar by clicking the little drop down arrow to the right of the last toolbar button, select "Add or Remove Buttons" and then click the buttons you want to add a tick to them. The button(s) you select will appear on your toolbar ...

enter image description here

Then you just select text and click the Increase Indent or Decrease Indent buttons. I tested this on Visual Studio 2013 only.

Автор: Ewan Размещён: 23.06.2015 01:39

1 плюс

17483 Репутация автора

With the Continuous Formatting extension (commercial, developed by me), the code is formatted really automatically as you type.

Автор: Sergey Vlasov Размещён: 10.07.2015 06:08

2 плюса

53 Репутация автора

Just to further Starwfanatic and Ewan's answers above. You can customise your IDE to add any button to any toolbar - so you can add the Format button (as the HTML Source Editing toolbar has) to any other toolbar (like Text Editing with all the other edit controls like increase/decrease indent).

Click the arrow to the right of the toolbar > Add or Remove Buttons > Customize... > Commands tab > Add Command... button.

Document Format and Selection Format are both under the Edit group.

(Tested in VS2010 and VS2013)

Автор: spaced - out Размещён: 07.01.2016 03:10

0 плюса

173 Репутация автора

Cut/Paste of a section is another quick way (and easy to remember).

Автор: David Anderson Размещён: 21.01.2016 05:44

5 плюса

7818 Репутация автора

Right click:

enter image description here

Works in VS 2015, maybe earlier version.

Автор: RayLoveless Размещён: 01.06.2016 05:08

0 плюса

127 Репутация автора

If you can afford it (or if you're eligible for the 30-day free trial) Jetbrains ReSharper can reformat a whole project directory.

Just install -> Right-click a directory -> select Cleanup Code from the context menu.

Автор: dcmorse Размещён: 07.09.2016 06:02

5 плюса

2031 Репутация автора

The original question said "I cannot find the setting."

Simple answer is: Look at top menu, then

Edit --> Advanced --> Format Document

You will also see the currently assigned key strokes for that function. Nothing special to memorize. This really helps if you use multiple developer environments on different operating systems.

Автор: zipzit Размещён: 06.02.2017 09:59

4 плюса

73 Репутация автора

In newer versions, the shortcut for the document-wide formatting is: Shift + Alt + F

Автор: Lucas Bernalte Размещён: 27.03.2017 02:12

25 плюса

288 Репутация автора

I have installed an extension named "Format document on Save" which formats the whole document every time you save it. For install it in VS15 or 17, on Tools just click the "Extensions and Updates...":

enter image description here

And then just go to "Online" at the left panel and search for "Format document on save" enter image description here

I hope it helps!.

Автор: Víctor Martínez Размещён: 28.03.2017 07:28

13 плюса

131 Репутация автора

  1. Go to Tools -> Extensions & Updates and type "productivity" in search: 1
  2. Install 'Productivity Power Tools 2015'
  3. Restart VS.
  4. Go to Tools -> Options -> Productivity Power Tools -> Power Commands and check "Format document on save": 2
Автор: Slava Latun Размещён: 18.09.2017 09:47

-1 плюса

14 Репутация автора

In Visual Studio 2015 and 2017 for c# code. 1) Scroll to the end of the file 2) Remove the last "Curly Bracket" } 3) Wait until the line above it shows an error 4) Replace the "Curly Bracket" } fini. :)

Автор: podzach Размещён: 26.10.2017 06:35

5 плюса

930 Репутация автора

In VS 2017 Format Document is CTRL E + D.

But...if you want to add the Format Document button to a tool bar do this.
Right click on tool bar.
Select "Customize.."
Select the "Commands" Tab.
Select the "Toolbar" radio button.
Select "Text Editor" from the pull down next to the radio button (or what ever tool bar you want the botton on)

Now...

Click the Add Command button.
Categories: Edit
Commands: Document Format
Click OK

Автор: Chris Catignani Размещён: 03.11.2017 08:10

10 плюса

185 Репутация автора

On mac : On Mac Shift + Option + F

On ubuntu : Ctrl + Shift + I

Автор: Jackson Smith Размещён: 12.11.2018 06:17

0 плюса

1 Репутация автора

#include "stdafx.h"
#include <stdio.h>


int main(){
    char filename[15];

    printf("Enter file name:");
    scanf("%s", filename);
    Automat(filename);
    freq(filename);
}

int Automat(char filename[]){

    FILE*fp;
    char c;
    int state = 1;
    int duma = 0;
    fp = fopen(filename, "r");
    if (fp == NULL){
        printf("Error");
        system("pause");
    }
    while (!feof(fp)){
        c = fgetc(fp);

        switch (state){
        case 1:
            if (c == '\t' || c == '\n' || c == ' '){
                state = 3;
                break;
            }
        case 2:
            if (c >='A'|| c<='Z'){
                break;
            }

        case 3: duma++;
            state = 1;
            break;
        }
    }
    printf("Broq e : %d\n", duma);
    return 1;
}
int freq(char filename[]){

    FILE*fp;
    char c;
    int state = 1, sequence = 0;
    fp = fopen(filename, "r");
    if (fp == NULL){
        printf("Error");
        system("pause");
    }



    while (!feof(fp)){
        c = fgetc(fp);
        switch (state){
        case 1:
            if (c >= 'A' && c <= 'Z'){
                state = 2;
                break;
            }

        case 2:
            if (c == '\t' || c == '\n'||c == ' '){
                sequence++;
                state = 1;
                break;

            }
        }

    }
    printf("Sequence is : %d\n", sequence);
}
Автор: Ivaylo Размещён: 03.04.2019 06:37

0 плюса

26 Репутация автора

You can also try right menu click option to format the selection of the coding document. Take a look at below screen shot

enter image description here

Автор: Suresh K Размещён: 10.06.2019 02:17

0 плюса

139 Репутация автора

I have a Mac and I clicked Code > Preferences > Settings > Workspace > Formatting and then selected Format On Save.

Now every time I hit cmd + s it auto formats the file.

enter image description here

Автор: supertramp Размещён: 11.06.2019 02:16

0 плюса

3578 Репутация автора

Select the text you want to automatically indent.

Click Format Selection in Edit, Advanced, or press CTRL+K, CTRL+F. Format Selection applies the smart indenting rules for the language in which you are programming to the selected text.

Step (1) :- CTRL+A

Step (2) :- CTRL+K

Step (3) :- CTRL+F

Автор: Keshav Gera Размещён: 13.06.2019 03:33

0 плюса

1 Репутация автора

Select the data and the right click and you will find this option. FORMAT DOCUMENT and FORMAT SELECTION

enter image description here

Автор: Ajit Mourya Размещён: 05.07.2019 10:36
Вопросы из категории :
32x32