Skip to content

Guides

Formatting automatically

Once your rules are settled, you shouldn’t have to think about formatting again. DressSharp can format just the files you’ve changed, so it’s quick enough to run on every commit or after every edit.

Each of these setups runs the version of DressSharp in your repository’s tool manifest (see Install). Run dotnet tool restore once after cloning.

The Rider plugin formats C# on save, as you type, or with Code | Format with DressSharp. Install its ZIP from a GitHub release, then choose when it formats from Rider’s settings.

The VS Code extension formats C# with Format document with DressSharp, or as VS Code’s default C# formatter for Format Document and format on save. Install its VSIX from a GitHub release, then choose DressSharp with Format Document With… and Configure Default Formatter….

A Git pre-commit hook can format the files you’re committing. Save this as .git/hooks/pre-commit:

#!/bin/sh
dotnet dress format --staged

--staged formats the working copies of files staged for this commit. If formatting changes any files, it exits with code 1 to block the commit. Review and stage the changes you want, then retry the commit. DressSharp leaves the index untouched, including when you’ve staged only part of a file; it still formats the whole working copy.

If you use a hook manager such as Husky.Net or pre-commit, give it the same command.

Coding agents rarely follow a style exactly. Run DressSharp each time an agent makes changes, and its code comes out formatted to your rules:

Terminal window
dotnet dress format --changed

--changed formats every file that differs from the last commit, including new files.

For Claude Code, add a hook to .claude/settings.json:

{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [{ "type": "command", "command": "dotnet dress format --changed" }]
}
]
}
}

For Codex, add the same hook to .codex/hooks.json:

{
"hooks": {
"PostToolUse": [
{
"hooks": [{ "type": "command", "command": "dotnet dress format --changed" }]
}
]
}
}

dotnet dress check changes nothing. It lists any files that don’t match your rules and exits with code 1, which fails the build. In GitHub Actions:

- uses: actions/setup-dotnet@v4
with:
dotnet-version: 10.0.x
- run: dotnet tool restore
- run: dotnet dress check

A new version of DressSharp can format some code differently. The release notes say when that happens. Because the version is pinned in your tool manifest, everyone upgrades together:

Terminal window
dotnet tool update DressSharp
dotnet dress init
dotnet dress

init adds any rules introduced since you last ran it, each set to its default, and leaves your existing settings alone. Commit the manifest, the EditorConfig and any reformatted files together.