Think Therefore

Muse, contemplate or ponder. Often best served with a drink. All I know is the world's a wonder… therefore I think.

Switching indenting formats in Visual Studio

in Web Development

The following is a simple macro that will allow you to switch indenting formats within Visual Studio. It’s only been tested in Visual Studio 2008 but it will allow you to quickly toggle between preserving tabs with a 2 space tab/indent (default) and preserving spaces with a 4 space tab/indent (alternative).

Once added you can then manually run the individual functions (DefaultIndenting or AltIndenting) or bind them to some hot keys.

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module Indenting

    Private Sub Toggle(ByVal bDefault As Boolean)
        Dim props As EnvDTE.Properties
        Dim ts As EnvDTE.Property
        Dim ins As EnvDTE.Property
        Dim tabs As EnvDTE.Property

        props = DTE.Properties("TextEditor", "AllLanguages")

        ts = props.Item("TabSize")
        ins = props.Item("IndentSize")
        tabs = props.Item("InsertTabs")

        If bDefault = True Then
            tabs.Value = True
            ts.Value = 2
            ins.Value = 2
        Else
            tabs.Value = False
            ts.Value = 4
            ins.Value = 4
        End If
    End Sub

    Sub DefaultIndenting()
        Toggle(True)
    End Sub

    Sub AltIndenting()
        Toggle(False)
    End Sub

End Module