Format (4 spaces) all code files in Visual Studio?

Viewed 127

I was working on an old branch feature/old-request, that branch did not have the .editorconfig file that informs Visual Studio to use 4 spaces (company agreed formatting).

So on my old branch, Visual Studio was using my own personal settings (2 spaces). Now that I am trying to merge branches (develop into feature/old-request), I'm getting a lot of changed files and the only change is the spacing.

I have now aborted the merge, then taken the same .editorconfig file from the develop branch and put it on my feature/old-request branch.

I don't fancy opening 100s of files and pressing Ctrl + K Ctrol + D.

Is there a way to tell Visual Studio to format all code files?

1 Answers

There is excellent tool in dotnet CLI for this: dotnet-format

It is also available for installation directly from command line:

dotnet tool install -g dotnet-format

Now, when you have your catalog with solution setup like this:

path to solution catalog
-- ConsoleApp
-- .editorconfig
-- ConsoleApp.sln

(this is just sample solution for example purposes).

Now, run command dotnet-foramt in solution directory and it will automatically apply settings from .editorconfig to your solution.

Related