<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans%3A300%2C400%2C600%2C700">EditorConfig code formatting from the command line with .NET Core's dotnet format global tool May 2019

EditorConfig code formatting from the command line with .NET Core's dotnet format global tool

  • March 21, 2019

EditorConfig helps maintain consistent coding styles for multiple developers working on the same project across various editors and IDEs.” Rather than you having to keep your code in whatever format the team has agreed on, you can check in an .editorconfig file and your editor of choice will keep things in line.

If you’re a .NET developer like myself, there’s a ton of great .NET EditorConfig options you can set to ensure the team uses consistent Language Conventions, Naming Conventions, and Formatting Rules.

  • Language Conventions are rules pertaining to the C# or Visual Basic language, for example, var/explicit type, use expression-bodied member.
  • Formatting Rules are rules regarding the layout and structure of your code in order to make it easier to read, for example, Allman braces, spaces in control blocks.
  • Naming Conventions are rules respecting the way objects are named, for example, async methods must end in “Async”.

If you’re using Visual Studios 2010, 2012, 2013, or 2015, fear not. There’s at least a basic EditorConfig free extension for you that enforces the basic rules. There is also an extension for Visual Studio Code to support EditorConfig files that takes just seconds to install.

ASIDE: If you are looking for a decent default for C#, take a look at the .editorconfig that the C# Roslyn compiler team uses. I don’t know about you, but my brain exploded when I saw that they used spaces vs tabs.

But! What if you want this coding formatting goodness at the dotnet command line? You can use “dotnet format” as a global tool! It’s one line to install, then it’s available everywhere for all your .NET Core apps.

D:githubhanselminutes-core> dotnet tool install -g dotnet-format

You can invoke the tool using the following command: dotnet-format
Tool 'dotnet-format' (version '3.0.2') was successfully installed.
D:githubhanselminutes-core> dotnet format
Formatting code files in workspace 'D:githubhanselminutes-corehanselminutes-core.sln'.
Found project reference without a matching metadata reference: D:githubhanselminutes-corehanselminutes.corehanselminutes-core.csproj
Formatting code files in project 'hanselminutes-core'.
Formatting code files in project 'hanselminutes.core.tests'.
Format complete.

You can see in the screenshot below where dotnet format used its scandalous defaults to move my end of line { to its own line! Well, if that’s what the team wants! 😉

Of course, dotnet format is all open source and up at https://github.com/dotnet/format. You can install the stable build OR a development build from myGet.

Sponsor: Manage GitHub Pull Requests right from the IDE with the latest JetBrains Rider. An integrated performance profiler on Windows comes to the rescue as well.

© 2018 Scott Hanselman. All rights reserved.


E-mail : [email protected]

      Submit A Comment

      Must be fill required * marked fields.