Formatting Projects
Formatting should be automatic and consistent.
Visual Studio
| Action | Default shortcut |
|---|---|
| Format document | Ctrl+K, Ctrl+D |
| Format selection | Ctrl+K, Ctrl+F |
To check your bindings, open Edit → Advanced. To change them, go to Tools → Options → Environment → Keyboard and bind Edit.FormatDocument and Edit.FormatSelection.
Format on Save
Go to Tools → Options → Text Editor → Code Cleanup and enable Run Code Cleanup profile on Save. Configure the profile to include Format document.
VS Code
Install the C# Dev Kit (or the C# extension). Its formatter follows your .editorconfig.
| Action | Windows | macOS | Linux |
|---|---|---|---|
| Format document | Shift+Alt+F | Shift+Option+F | Ctrl+Shift+I |
| Format selection | Ctrl+K, Ctrl+F | Cmd+K, Cmd+F | Ctrl+K, Ctrl+F |
Format on Save
Add the following to .vscode/settings.json:
{
"[csharp]": {
"editor.defaultFormatter": "ms-dotnettools.csharp",
"editor.formatOnSave": true
}
}