Tool/trick to replace tabs in all files of an entire VS solution

In case if your files contains spaces as tabs of equal width all along the solution (i.e. 4 spaces per tab), then you should use plain VS's Quick replace tool. In order to follow StyleCop's formatting rules, you should use spaces instead of tabs, so go to Quick replace (CTRL-h), select Use wildcard and then in Find what field you can use \t symbol, replacing it with 4 spaces for all solution (Look in field).

But if your solution files contains tabs made of spaces of different width, or you want to apply single-style formatting, then you definitely should use Resharper's feature to reformat code. This feature is called Code Cleanup. To apply code cleanup to whole solution or selected project, go to the solution explorer and select Cleanup code in context menu. But before launching reformatting on whole solution try and tweak it on one file, there's a lot of options in Resharper's settings.


A simple and fast way is to open file in notepad++ and either replace it manually (switch on 'show all characters' and tabs will be highlighted), or replace it automatically (have to play with a 'Settings')


I keep a unix tool directory in my path when working in Windows, for just such emergencies. For instance, this would replace all tabs with 4 spaces, in any .cs files in the c:\myproject directory or it's subdirectories.

find c:\myproject -name *.cs -exec sed -i -e "\"s/\t/    /g\"" {} ";"

there you go:

using System;
using System.IO;

class _Runner {
  static void Main(string[] args) {

    var root=args[0];
    var filePaths = Directory.GetFiles(root, "*.cs", SearchOption.AllDirectories);

    int updated = 0;
    foreach (var path in filePaths) {
      var content = File.ReadAllText(path);
      var replaced = content.Replace("    ", "\t");
      if (replaced == content) { 
        continue;
      }

      ++updated;
      Console.WriteLine("fixing " + path);
      File.WriteAllText(path, replaced);
    }

    Console.WriteLine("fixed {0} files", updated);  
  }
}

Save is as spaces-to-tabs.cs, and run:

C:>c:\Windows\Microsoft.NET\Framework\v3.5\csc spaces-to-tab.cs
C:>spaces-to-tabs.exe C:\path\to\your\solution