I just discovered dotnet-format, but as far as I understand it's a command line tool that has to be called manually. How can I apply dotnet-format on saving a file in Visual Studio 2019? (not Visual Studio Code!)
Although this is not what the question was about, I have dotnet-format setup to run as a pre-commit hook.
#!/bin/sh
dotnet-format
exit 0
For the on-save formatting and other gimmicks, I use CodeMaid. https://marketplace.visualstudio.com/items?itemName=SteveCadwallader.CodeMaid
I've gathered all the instructions in one place for multiple IDEs, including Visual Studio, to make them easier to find and use
.onsaveconfig file in the root folder in your project[*.cs]
command = dotnet
arguments = format "{solution_directory}\YourProjectName.sln" --include {filename} --verbosity diagnostic
YourProjectName.sln with your actual solution file name ({solution_directory} and {filename} remain unchanged)--verbosity diagnostic if you don’t need detailed logs (see docs)*.cs file (especially when always_run is disabled) and save it, then wait a few seconds or minutesThe formatting should be applied automatically.
If the instructions above don't work, try the following troubleshooting steps:
dotnet format in your project. If the command fails, the issue is likely with the tool itself (e.g., not installed, incorrect .editorconfig configuration, or another setup problem)
Special thanks to waf for this extension!
.vscode/settings.json file in the root folder in your project{
"emeraldwalk.runonsave": {
"commands": [
{
"match": "\\.cs$",
"cmd": "dotnet format ./YourProjectName.sln --include ${relativeFile} --verbosity diagnostic"
}
]
}
}
./YourProjectName.sln with your actual solution file name (everything else remains unchanged)This article offers a solid guide to setting up formatting (see the Dotnet Format section). However, some parts are slightly outdated. The main difference lies in the arguments:
dotnetformat $SolutionPath$ --include $FileName$ --verbosity diagnosticEverything else remains the same
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With