Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 801
Flores Avatar asked Nov 10 '10 09:11

Flores


People also ask

How do you replace all tabs with spaces in Visual Studio?

Change Tab to use "Insert Spaces" instead of "Keep Tabs". Note you can also specify this per language if you wish to have different behavior in a specific language. Show activity on this post.

How do you replace all tabs?

Replace all tabs Note that your search and replace (ctrl-r) will always convert a tab to a fixed number of spaces. It does not do a tabulation where you can line things up from one line to the next by using a tab.

How do I Find and replace tabs in Vscode?

To search for tabs enter [\t] in Find box. Enter spaces in Replace box and perform your replace.

How do you replace all in VS code?

You can also Search and Replace across files. Expand the Search widget to display the Replace text box. When you type text into the Replace text box, you will see a diff display of the pending changes. You can replace across all files from the Replace text box, replace all in one file or replace a single change.


2 Answers

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.

like image 68
Dmitrii Lobanov Avatar answered Oct 02 '22 15:10

Dmitrii Lobanov


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 
like image 33
Ken Egozi Avatar answered Oct 02 '22 14:10

Ken Egozi