Tutorial
- Code Formatting In Visual Studio 2019
- Code Formatting In Visual Studio Download
- Reformat Code In Visual Studio
While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or edited it to ensure you have an error-free learning experience. It's on our list, and we're working on it! You can help us out by using the 'report an issue' button at the bottom of the tutorial.
Introduction
Formatting code consistently is a pain, especially when working on a team. The beauty of modern day web development is that the tooling has gotten so much better! In this article, we will look at setting up Prettier to automatically format your code in Visual Studio Code.
Visual Studio 2017 version 15.3 Option values true - Sort System. using directives alphabetically, and place them before other using directives. Formatters extensions for Visual Studio Code on marketplace.visualstudio.com. Editor behaviors can be set to allow code to be formatted as it is written. These actions are set under Visual Studio Preferences Text Editor Behavior, and some of the more commonly used functions are described below: Matching closing braces can be added automatically to code when creating new classes, methods, or properties.
Ctrl + E, Ctrl + D to format the entire document. Ctrl + E, Ctrl + F to format the selection. You can change these in menu Tools → Options → Environment → Keyboard (either by selecting a different 'keyboard mapping scheme', or binding individual keys to the commands 'Edit.FormatDocument' and 'Edit.FormatSelection'). Option 1 Go to any.cs file and open in code editor. Right click on the code file and you will get option to 'Format Code' as shown below. You can also invoke this command using Keyboard shortcut 'Ctrl+D, Ctrl+F'.
Sample Code
For demo purposes, here’s the sample code we will be formatting. If you’re picky about code formatting, you’ll pick up on some obvious misteps immediately.
- mix of single vs double quotes
- the first property of the person object should be on it’s own line
- the console statement inside of the function should be indented
- you may or may not like the optional parenthesis surrounding the parameter of the arrow function
Installing the Prettier Extension
To work with Prettier in Visual Studio Code, you’ll need to install the extension. Search for Prettier - Code Formatter. You can see the extension below. If you’re installing it for the first time, you’ll see an “install” button instead of the “uninstall” button you see on mine.

The Format Document Command
With the Prettier extension installed, we can now leverage it to format our code. We’ll work more on this later, but to start, we can use the Format Document command.
To open the command pallette, you can use Command + Shift + P on Mac or Control + Shift + P on Windows. In the command pallette search format, then choose Format Document.

You may then be prompted by to choose which formatter to use. To do so, click the Configure button.
Then choose Prettier - Code Formatter.
And then VOILA! Your code is nice and formatted. Notice all the fancy improvements!
- spacing
- line wrappings
- consistent quotes
Prettier also works with CSS files!
The awesome thing is that this also works on CSS files!
From this…
To this!
Automatically Format on Save
So far, we have had to manually run a command to format our code. Instead, you can choose a setting in VS Code to have your files automatically formatted when you save. This has some great benefits.
You never have to manually format your code again!
- ensure code is formatted without having to think about it
- code doesn’t get checked in that’s not formatted
To change this setting, use Command + , on Mac or Control + , on Windows to open the settings menu. Then search for Editor: Format on Save and make sure it is checked.
With this setting in place, you can go about your business writing sloppily formatted code like we all do knowing that it will all be taken care of automatically for you!
Prettier Configuration in VS Code Settings
Prettier does a lot of things for you by default, but you can also customize the settings. Here are a few of the most common settings.
- Single Quote - choose between single and double quotes
- Semi - choose whether or not to include semi colons at the end of lines
- Tab Width - how many spaces you want a tab to consist of
Open the settings menu as above. Then, search for Prettier. This will bring up all of the settings that you can change right there in your editor.
For example, what if I change the tab width to 10.
Then save my file.
Pretty easy right?! This is probably not the tab width size you want to keep, but it’s all up to you!
Creating a Prettier Configuration File
The downside to using the built-in settings menu in VS Code is that it doesn’t ensure consistency across developers on your team. If you change settings in your VS Code, someone else could have an entirely different set of settings in theirs.
Establish consistent formatting across your team by creating a configuration file!
To solve this, you can create a Prettier configuration file. It has to be titled .prettierrc.(ext) with one of the following extensions.

- yml, yaml, or json
- js
- toml
- include in package.json file (alternate option)
I typically prefer JSON configuration files where you can define key -> value pairs for your settings. VS Code will even provide some intellisense for you as you type.

Here’s an example of a simple configuration file.
For more specifics on the configuration files, check out the Prettier Docs. After creating one of these and checking it in to your project, you can ensure that every team member follows the same formatting rules.
Conclusion
Don’t waste your time manually formatting your code. It takes time that can be better spent writing more code. Take advantage of the amazing modern tools out there and set up Prettier!
Format the class file and arrange fields, properties and methods as per StyleCop rules.
Features
- Format the class file (.cs) to arrange members of class in the proper order.
- Using statements are brought inside namespace block.
- Unwanted namespaces are removed.
- Unwanted space are removed.
- Proper space introduced where ever required.
- Adds header comments on members of the class.
- Modifies the comment on the members if signature of the member is changed
- Can group members by regions
Feature Format class
Members of the class are arranged in following order
- Constants
- Fields
- Constructors
- Properties
- Methods
Furthermore the members are also sorted by access modifiers as follows
- public
- internal
- protected
- private
How to use
After installation can execute format code command from two different place.
Code Formatting In Visual Studio 2019
Option 1
- Go to any .cs file and open in code editor.
- Right click on the code file and you will get option to 'Format Code' as shown below.
- You can also invoke this command using Keyboard shortcut 'Ctrl+D, Ctrl+F'
Option 2
- Go to solution explorer
- Select the .cs file which needs to be formatted
- Right click on the file and you will get 'Format Code' option on the menu as shown below.
Option 3
- Go to solution explorer
- Select the any project which needs to be formatted
- Right click on that project and you will get 'Code Formatter > Format Code' option on the menu as shown below.
Option 4
- Go to solution explorer
- Select the solution node
- Right click on that solution node and you will get 'Code Formatter > Format Code' option on the menu as shown below.
In Option 3 and 4, all the .cs file present within the Project or solution respectively will get formatted automatically.
Settings
Settings for tool can be accessed from Visual Studio options screen as shown below.
General Settings
Type Layout
You can customize the layout of the class or interface by setting required layout under Type layout setting
Note: That the first time when this extension is installed, type layout will be blank. Hence first time user will have to configure the type layout, and you can also set type layout with the help of the default templates.
Code Formatting In Visual Studio Download
File Header
You can also choose to include class header by updating setting under File Header.
You can customize the file header format and you can use the available replacement to format the header.
You can also have the different file header template based on the solution or project also.
To have customized file header for a perticular solution, go to Solution and right click and go to 'Code Formatter >> Add Header Setting File'. And you can set perticular format in that setting file.
Same is applicable of project level file header as well.
Note: File template selection will first check for project header setting file, if not found then it will look for solution header setting file and if the file is not present then global header will be applied.
Download this extension from the VS Gallery
Installation Guide:
1. If you are installing it manually using VSIX file then make sure you uninstall the previous version anf then install the new version. Because if at the same time multiple version are installed of the same extension then extension will be disabled by Visual Studio.
2. It is advisable to install the extension from Tools > Extensions and Update option.
Reformat Code In Visual Studio
License
