Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a solution-wide way to Untabify the whole files

I have downloaded a project and it is all using Tab , but I always use Space. There is the option that "Edit->Advanced->Untabify" but for then I have to select files one by one and do this for all of them, so I was wondering if there a way that I can apply thzt to the whole files in the project. I am using C# and VS2010 Pro. I also have Resharper installed, in case it has something that can help.

like image 294
Bohn Avatar asked Aug 07 '12 12:08

Bohn


People also ask

How do you Untabify?

Press F1 and look for the command Untabify and hit Enter or press Ctrl-T Ctrl-U .

What is Tabify in Visual Studio?

tabify - convert the spaces in the selected texts into tabs.


2 Answers

Your best bet is to use the Find and Replace in files (ctrl-shift-h). Turn on regular expressions, search for \t and replace with spaces. You can set the file filter to *.cs or whatever file type you need to clean up.

like image 177
Erick T Avatar answered Nov 05 '22 17:11

Erick T


I created a simple console app that untabify all files from a base directory.

https://github.com/idenardi/TabsToSpace

Here is the code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace TabToSpaces
{
    class Program
    {
        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.ReadAllLines(path);
                string[] newfile = new string[content.Length];

                int i = 0;
                var bChanged = false;

                foreach (var line in content)
                {
                    newfile[i] = ReplaceTabs(line, out var bChangedLine);
                    i++;

                    if (bChangedLine)
                        bChanged = true;
                }

                if (bChanged)
                {
                    ++updated;
                    Console.WriteLine("fixing " + path);
                    File.WriteAllLines(path, newfile, System.Text.Encoding.UTF8);
                }
            }

            Console.WriteLine("fixed {0} files", updated);
            var x = Console.ReadKey();
        }


        public static string ReplaceTabs(string line, out bool bChanged)
        {
            bChanged = false;
            var i = 0;
            var lstIndex = new List<int>();

            foreach(char c in line)
            {
                if (c == ' ')
                    continue;
                else if (c == '\t')
                    lstIndex.Add(i);
                else
                    break;

                i++;
            }

            var lstIndexOrdered = lstIndex.OrderByDescending(c => c);
            foreach (var index in lstIndexOrdered)
            {
                line = line.Remove(index, 1).Insert(index, "    ");
                bChanged = true;
            }

            return line;
        }
    }
}

To convert you need call this app passing the base directory as the first argument:

TabToSpace.exe C:\Projects\MyBaseDirectory

like image 28
idenardi Avatar answered Nov 05 '22 16:11

idenardi