Use tabs instead of spaces in .csproj file in Visual Studio 2022

44 Views Asked by At

When I'm creating a new project file, it generates a new .csproj file where single tabs are replaced with two spaces.

default behaviour

I can manually replace them through Edit->Advanced->Format Document (Ctrl+E, D), but I want to make it automatically when I'm creating a new one.

wished behaviour

I tried to mess with tabs in options, but it's not working. How can I make it work?

2

There are 2 best solutions below

3
PMF On

This can be configured in the .editorconfig file in your project. These settings use tabs on C# files, but spaces on csproj files:


[*.{cs}]
charset = utf-8-bom
indent_style = tab
tab_width = 4

[*.{csproj}]
charset = utf-8-bom
indent_style = space
indent_size = 2
tab_width = 2

1
Timur Vafin On

Thanks to @GuruStron. I had to manually unzip each project template which is located at C:\Program Files\dotnet\templates\8.0.2. Edited double spaces to single tab in each .csproj from package, and then zip them again.

Asked ChatGPT to write powershell script to save my life:

# We are looking for all files with the extension .csproj in the current folder and its subfolders
$files = Get-ChildItem -Path . -Recurse -Filter *.csproj

# Loop through the found files
foreach ($file in $files) {
 # Reading the contents of the file
 $content = Get-Content $file.FullName

 # Replacing double spaces with tabs
 $newContent = $content -replace ' ', "`t"

 # Writing the changed content back to the file
 Set-Content -Path $file.FullName -Value $newContent
}

Write-Output "Spaces have been successfully replaced with tabs in files with the extension .csproj"