I am interested in formatting all the files in a Visual Studio (ver. 2005) project all at once.
Currently, there is a way to format a single document by doing something like Edit->Advanced->Format Document. However, I don't see a single command to format all the files of a project all at once.
Any idea how to do that?
Additional step needed for Visual Studio 2015
Phil Haack's solution posted by ANeves is perfect, but for some reason
$item.FileCodeModel.Language
always returns null in Visual Studio 2015 making theFormat-Document
skip all files and effectively do nothing.To (hackily) work around this limitation you can replace the
Get-Language
function:with the following variant that uses the file's extension instead of the Language GUID:
Note, that the following solution does not work by itself starting with Visual Studio 2015. You need to apply the answer by Marcus Mangelsdorf as well. Then, this script works in Visual Studio 2015 and 2017.
Phil Haack outlined a good procedure - adding a reusable script to indent the project.
Open your NuGet profile for edition
$profile
to see the location of your NuGet profile;mkdir –force (split-path $profile)
to create the profile's folder if it does not exist;notepad $profile
.Add the reusable method to the NuGet profile
Phil used davidfowl's
Format-Document
method which he found at https://gist.github.com/davidfowl/984358:Reopen Visual Studio to use the command
When you reopen Visual Studio, the command is available.
Simply run it from the NuGet Package Manager Console:
Format-Document
This will re-format all files of the selected project.To apply to the whole solution, use the command
Get-Project -All | Format-Document
, which lists the projects and then for each of them calls the reformatting command.As the author put it:
10/10, would run again.
Tim Abell wrote a macro to do this on his blog:
The original code is available at the blog post. Note that this is older than the version available on github above.
The Format All Files extension worked for me. Nothing to do, just install and click!