Formatting In Visual Studio Code



Instead of formatting the code while saving or by applying the key combination Ctrl + K, Ctrl + F or Alt + Shift + F, I need to format the current line of code while hitting the Enter key. This feature is already available in Visual Studio, but not found in Visual Studio Code. Formatters extensions for Visual Studio Code on marketplace.visualstudio.com.

Visual Studio Website Examples

Visual Studio Code Formatters Salesforce XML Formatter New to Visual Studio Code? You can use the Formatting actions of VS Code: Format Document and Format Selection; or just use the configuration showed above in the Auto Format section to format on save. Extension Settings. This video slipped through unedited. We are keeping it here as it's still useful, but sorry for the unpolished nature of it!If you enjoyed this video.

-->Formatting In Visual Studio Code

Use the Formatting options page and its subpages (Indentation, New Lines, Spacing, and Wrapping) to set options for formatting code in the code editor.

Formatting In Visual Studio Code Example

To access this options page, choose Tools > Options from the menu bar. In the Options dialog box, choose Text Editor > C# > Code Style > Formatting.

Tip

The Indentation, New Lines, Spacing, and Wrapping subpages each display a preview window at the bottom that shows the effect of each option. To use the preview window, select a formatting option. The preview window shows an example of the selected option. When you change a setting by selecting a radio button or check box, the preview window updates to show the effect of the new setting.

Auto Formatting In Visual Studio Code

StudioFormatting In Visual Studio Code

Formatting (General) page

General settings

Formatting In Visual Studio Code

These settings affect when the code editor applies formatting options to code.

LabelDescription
Automatically format when typingWhen deselected, the format statement on ; and format block on } options are disabled.
Automatically format statement on ;When selected, formats statements at completion according to the formatting options selected for the editor.
Automatically format block on }When selected, formats code blocks according to the formatting options selected for the editor as soon as you complete the code block.
Automatically format on returnWhen selected, formats text when Enter is pressed, to fit the formatting options selected for the editor.
Automatically format on pasteWhen selected, formats text that is pasted into the editor to fit the formatting options selected for the editor.

If you previously applied code style settings for C# files using the Format Document command in Visual Studio 2017, that functionality is now available as Code Cleanup.

Format Document settings

Formatting In Visual Studio Code

These settings configure the Format Document command to perform additional code cleanup on a file. For more information about how these settings are applied, see Format Document command.

LabelDescriptionCorresponding EditorConfig and Tools > Options rules
Apply all C# formatting rules (indentation, wrapping, spacing)The Format Document command always fixes formatting issues. This setting can't be changed.Core EditorConfig options
.NET EditorConfig formatting options
Tools > Options > Text Editor > C# > Formatting > [Indentation or New Lines or Spacing or Wrapping]
Perform addition code cleanup during formattingWhen selected, applies fixes for the rules specified below on the Edit.FormatDocument command.N/A
Remove unnecessary usingsWhen selected, removes unnecessary using directives when Edit.FormatDocument is triggered.N/A
Sort usingsWhen selected, sorts using directives when Edit.FormatDocument is triggered.dotnet_sort_system_directives_first
Tools > Options > Text Editor > C# > Advanced > Place 'System' directives first when sorting usings
Add/remove braces for single-line control statementsWhen selected, adds or removes braces from single-line control statements when Edit.FormatDocument is triggered.csharp_prefer_braces
Tools > Options > Text Editor > C# > Code Style > Code block preferences > Prefer braces
Add accessibility modifiersWhen selected, adds missing accessibility modifiers when Edit.FormatDocument is triggered.dotnet_style_require_accessibility_modifiers
Sort accessibility modifiersWhen selected, sorts accessibility modifiers when Edit.FormatDocument is triggered.csharp_preferred_modifier_order
visual_basic_preferred_modifier_order
Apply expression/block body preferencesWhen selected, converts expression-bodied members to block bodies, or vice versa, when Edit.FormatDocument is triggered.Expression-bodied member EditorConfig options
Tools > Options > Text Editor > C# > Code Style > Expression preferences > Use expression body for methods, constructors, etc.
Apply implicit/explicit type preferencesWhen selected, converts var to the explicit type, or vice versa, when Edit.FormatDocument is triggered.Explicit type EditorConfig options
Tools > Options > Text Editor > C# > Code Style > 'var' preferences
Apply inline 'out' variables preferencesWhen selected, inlines out variables where possible when Edit.FormatDocument is triggered.csharp_style_inlined_variable_declaration
Tools > Options > Text Editor > C# > Code Style > Variable preferences > Prefer inlined variable declaration
Apply language/framework type preferencesWhen selected, converts language types to framework types, or vice versa, when Edit.FormatDocument is triggered.dotnet_style_predefined_type_for_locals_parameters_members
dotnet_style_predefined_type_for_member_access
Tools > Options > Text Editor > C# > Code Style > predefined type preferences
Apply object/collection initialization preferencesWhen selected, uses object and collection initializers where possible when Edit.FormatDocument is triggered.dotnet_style_object_initializer
dotnet_style_collection_initializer
Tools > Options > Text Editor > C# > Code Style > Expression preferences > Prefer object initializer or Prefer collection initializer
Apply 'this.' qualification preferencesWhen selected, applies this. preferences when Edit.FormatDocument is triggered.this. qualification EditorConfig options
Tools > Options > Text Editor > C# > Code Style > 'this.' preferences
Make private fields readonly when possibleWhen selected, makes private fields readonly where possible when Edit.FormatDocument is triggered.dotnet_style_readonly_field
Tools > Options > Text Editor > C# > Code Style > Field preferences > Prefer readonly
Remove unnecessary castsWhen selected, removes unnecessary casts where possible when Edit.FormatDocument is triggered.N/A
Remove unused variablesWhen selected, removes variables that are unused when Edit.FormatDocument is triggered.N/A

Formatting In Visual Studio Code

Indentation page

Format In Vs Code

The indentation options on this page apply when code is formatted automatically. One example of when code is automatically formatted is when you paste code into the file while Automatically format on paste is selected. (The Automatically format on paste option is under Formatting > General.)

Tip

There are also indentation options on the Text Editor > C# > Tabs options page. Those options only determine where the code editor places the cursor when you press Enter at the end of a line.

See also