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

Visual StudioVisual Studio-2010

Visual Studio Problem Overview


My solution (30+ project) is a bit of a mess when it comes to mixing tabs and spaces, and I want to fix this with one easy step.

Does anybody know a trick/tool to do this in one step for the entire solution?

EDIT: Not exactly what I meant. I want the documents to be nicely formatted. Just find/replace wont do.. documents will still be a mess. I want something like the Format -> Advanced -> Format Document command

Visual Studio Solutions


Solution 1 - Visual Studio

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.

Solution 2 - Visual Studio

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

Solution 3 - Visual Studio

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\"" {} ";"

Solution 4 - Visual Studio

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')

Solution 5 - Visual Studio

Not a perfect solution, but the extension "Fix Mixed Tabs" is a huge help https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.FixMixedTabs

Basically when you open a file that has mixed tabs/spaces whitespace, it popup up a toolbar at the top of the file to allow you to normalize it

Solution 6 - Visual Studio

ctrl-h, set "Look in:" to "entire solution"

Also you can record a macro.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFloresView Question on Stackoverflow
Solution 1 - Visual StudioDmitrii LobanovView Answer on Stackoverflow
Solution 2 - Visual StudioKen EgoziView Answer on Stackoverflow
Solution 3 - Visual StudioMudView Answer on Stackoverflow
Solution 4 - Visual StudioSlavView Answer on Stackoverflow
Solution 5 - Visual StudioFiniteLooperView Answer on Stackoverflow
Solution 6 - Visual StudioAbyxView Answer on Stackoverflow