Convert Spaces To Tabs For A Visual Studio Solution or Project

I recently installed some VS.Net power tools, and 1 of the tools is something that tells you when you have a file with mixed tabs and spaces, giving you the chance to convert from one to another.

I decided to stick with Tabs (I know that spaces v. tabs is a religious war for many, but I won’t go into it).  The problem is that this only happens on a file by file basis.  Furthermore, when you change the whitespace, your source controls sees that lots and lots of lines in your file have changed, making it hard to see the 1 or 2 lines that ACTUALLY changed.

I didn’t find a single utility out there for converting spaces to tabs, so after some poking around I’ve come up with a workaround that does the trick, even if it is a bit hackish.

I created a Find/Replace regular expression that will find leading groups of 4 spaces and replace them with tabs.  The problem is: you have to run it recursively.  So if you have a spot in your code with 10 tabs of indentation, you’ll need to run the find/replace 10 times in a row, which each iteration converting 1 of the groups of 4 spaces to a tab.

Each time you run the Find/Replace the count will get smaller and smaller, until finally: 0.

The basic pattern you search for is:

^{(t)*}([ ]^4)

and you replace with:


In this example, the ^4 means that I’m treating 4 spaces as a tab.  Change the number if you want to have 2 spaces = a tab or whatever.

Another thing you can do to make it run faster on a large project is to first try to replace large sections of spaces before replaces 4 spaces at a time.  For example, the following stuff will search for 20 spaces and replace with 5 tabs.

You search for:

^{(t)*}([ ]^20)

and you replace with:


This way, if you have 1 spot in your code with 60 spaces, you won’t have to run the replace 15 times.



14 thoughts on “Convert Spaces To Tabs For A Visual Studio Solution or Project

  1. Genius! Just opened a project to find 2 spaces for indentation and my blood instantly boiled. What a nazi fascist thing to do.

    Why not just use tabs? My tabs show the width of 4 spaces, set yours to 2 and now we both have our cake and can eat it too.

    This regex worked like charm (using multiples of 2). Thank you!!!!

  2. I have tried this and found a couple of challenges. Not sure about previous version of Visual Studio but in 2010 but I have use \t for the substitution and I can only get it to do the first tab in a line. After the first round it does not find any more items to replace even though there are groups of spaces still left on the lines. Has something changed?

  3. I was trying this today and ran into a couple of challenges. Not sure if its formatting or what but the article says 1t for one tab, but I’ve found that needs to be /t. Also when I run it it only works for the first x number of spaces. After that it no longer finds any items to replace. I’m using this in Visual Studio 2010, has something changed or am I missing something? Thanks


    • Yea it appears to have changed.
      I just tried it in VS 2012 and I needed to use ^[ ]{4} for the search term, and \t for the replacement. You’ll need to run this multiple times as you noticed it only gets the FIRST set of spaces at the start of the line. BTW, it looks like vs 2012 doesn’t keep tabs, and converts them to spaces automatically, even when I add tabs intentionally in my code or html documents.

      • Chris,

        I can’t speak for 2012 as I don’t have a copy but in 2010 that doesn’t work either. I admit that Regular Expressions are not my forte but I don’t get that to work either. I tried ^([ ]^4) but that only ever does the first 4 spaces on a line and nothing else. I then tried ^{\t*}([ ]^4) with a replace expression of \1\t which based on everything I read should work. But it only ever puts 1 tab on the line. I can see the spaces just disappear when I repeat the search and replace. I tried some other various combinations but have not been able to get anything to work properly or at least as I would expect it.

        If anybody else happens to come up with a working solution for 2010 … I’m all ears.


  4. I tested this in VS 2010. Find ^{(\t)*}([ ]^4) and replace with \1\t. You’ll have to run it multiple times, each time it will replace 1 more level of tabs. So if you have some stuff that is 24 spaces worth of indention (6 tabs) you’ll need to run the find/replace 6 times consecutively.

    • Well don’t know whats different on my end but I’ve tried that and it only ends up with one tab and every thing left justified. I’ve got Visual Studio 2010 Version 10.0.40219.1 SP1Rel. I even went in and reset all my Visual Studio settings back to the default for C#.

      • hmmm … it must have something to do with different file extentions. I was trying it on a .cst file because I have a bunch of files with that extention that I need to change the tabs to. When I rename one of those files to .cs …it works!!!! I don’t want to have to go through the process of renaming all the files but I will if I have to. I’ll let you know what I find out.

  5. So I had the cst file type designated as a “Script Editor” in the “Editor Experience”. Once I changed it to “Microsoft Visual C#” or “Script Editor w/Encoding” the substitutions worked just fine. Thanks Again

  6. I found that the easiest way have this actually work was:
    Find what: {(t)*}([ ]^4)
    Replace with: \t
    Look in: Entire Solution
    Use: Regular Expressions

    The ^ at the start of the RegExp means to only match if it happens at the start of a line or group. Remove that and rather than replacing only the first entry, it will replace it all in one easy swoop. 644 instances replaced in my XML without needing to do any heavy lifting.

  7. Standard editor magic to handle this,
    Ctrl+A (Select all)
    Tab (Adding a tab to all lines, depending upon the editor (VS 2010 in my case) will convert all leading spaces to tabs)
    Shift+Tab (Undo the tab you just added)
    Then just cleanup if needed any lines that select all didn’t cover. (In my case this was just lines that contained only spaces…)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s