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
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.
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.
To search for tabs enter [\t] in Find box. Enter spaces in Replace box and perform your replace.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With