Tabifying/Untabifying Multiple Files in Visual Studio

Visual Studio provides the possibility to tabify/untabify selected lines out-of-box (Edit > Advanced > Tabify Selected Lines for tabifying or Edit > Advanced > Untabify Selected Lines for untabifying). That is a nice feature, but it works only on per-file basis. So, how to process multiple files in a solution?

The easiest way is using the standard Find and Replace window: (Edit > Find and Replace > Replace in Files or with keyboard shortcut Ctrl + Shift + H). What we want to do is simply replace tab symbols with desired number of spaces:

Specifying parameters for untabifying in Find and Replace window
Specifying parameters for untabifying in Find and Replace window

Make sure “Use Regular Expressions” box is selected, otherwize the “\t” string will be treated literally, not as the tab symbol.

Pros: free, quick, undoable, reversible (e.g. replacing four spaces with a tab and then a tab with four spaces leaves the file in unaltered state), customizable (e.g. file location and types).

Cons: spaces and tabs from the whole line are affected, not only the ones in the beginning. That means, comments placed on the end of the line may not be nicely aligned after replacement. That should not be a problem if your C# code conforms C# Coding Conventions, which encourage placing comments on separate lines, not at the end of lines of code.

It is still possible to replace symbols only from the beginning of lines by adding a “^” symbol to the regex expression in the Find what box, but in this case replacement will require multiple iterations (one iteration per nesting level).

First iteration:

Specifying parameters for untabifying in Find and Replace window
Specifying parameters for untabifying in Find and Replace window, first iteration

Second iteration (note increased number of spaces in both Find what and Replace with boxes):

Specifying parameters for untabifying in Find and Replace window
Specifying parameters for untabifying in Find and Replace window, second iteration

…and so on.

This method provides better results, but requires a bit more manual work and is more error-prone.