Advanced > Tabify Selected Lines (and the Power Tools 2010 also provide this functionality on a per-file basis) but is there a way to do this for all code" />
开发者

"Tabify" all files in Visual Studio solution?

There's a "tabify" command in

Edit > Advanced > Tabify Selected Lines

(and the Power Tools 2010 also provide this functionality on a per-file basis) but is there a way to do this for all code files in a solution?

ReSharper has a Clean Up command but the only half-suitable option I found there is to run formatting on all files which do开发者_运维百科es more than I want (I don't want to run a complete formatting, just tabifying).


If you have added the Microsoft Productivity Power tools extension (which if you haven't I would recommned) it adds an option to tabify files. This does not apply across all files in a solution, but it's prompted for when editing each file, on a per file basis. Not quite what you're after but a help.

Also you might try setting your IDE editor settings to use tabs, then do menu-edit-advanced-format document (CTRL+E,D). This will replace groups of tab length spaces with a tab, and that should be scriptable for all files in the solution via a macro.


The request contains links to IDE macros that can do the job:
http://blogs.msdn.com/b/kevinpilchbisson/archive/2004/05/17/133371.aspx
http://web.archive.org/web/20090217094033/http://chriseargle.com/post/Format-Solution.aspx

Here is sample code for a Visual Studio macro that automatically formats all *.cs, *.h, *.cpp, and *.hpp files in an open solution, which includes converting spaces to tabs (depending on your Tab settings in Tools > Options > Text Editor > specific language or "All Languages" > Tabs):

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module ConvertTabsToSpaces

    Public Sub FormatSolution()
        Dim sol As Solution = DTE.Solution
        For i As Integer = 1 To sol.Projects.Count
            FormatProject(sol.Projects.Item(i))
        Next
    End Sub

    Private Sub FormatProject(ByVal proj As Project)
        If Not proj.ProjectItems Is Nothing Then
            For i As Integer = 1 To proj.ProjectItems.Count
                FormatProjectItem(proj.ProjectItems.Item(i))
            Next
        End If
    End Sub

    Private Sub FormatProjectItem(ByVal projectItem As ProjectItem)
        If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then
            If projectItem.Name.LastIndexOf(".cs") = projectItem.Name.Length - 3 Then
                Dim window As Window = projectItem.Open(Constants.vsViewKindCode)
                window.Activate()
                projectItem.Document.DTE.ExecuteCommand("Edit.FormatDocument")
                window.Close(vsSaveChanges.vsSaveChangesYes)
            ElseIf ((projectItem.Name.LastIndexOf(".cpp") = projectItem.Name.Length - 4) OrElse (projectItem.Name.LastIndexOf(".hpp") = projectItem.Name.Length - 4) OrElse (projectItem.Name.LastIndexOf(".h") = projectItem.Name.Length - 2)) Then
                Dim window As Window = projectItem.Open(Constants.vsViewKindCode)
                window.Activate()
                projectItem.Document.DTE.ExecuteCommand("Edit.SelectAll")
                projectItem.Document.DTE.ExecuteCommand("Edit.FormatSelection")
                window.Close(vsSaveChanges.vsSaveChangesYes)
            End If
        End If

        'Be sure to format all of the ProjectItems.
        If Not projectItem.ProjectItems Is Nothing Then
            For i As Integer = 1 To projectItem.ProjectItems.Count
                FormatProjectItem(projectItem.ProjectItems.Item(i))
            Next
        End If

        'Format the SubProject if it exists.
        If Not projectItem.SubProject Is Nothing Then
            FormatProject(projectItem.SubProject)
        End If
    End Sub

End Module

Instructions (Visual Studio 2005, but similar for newer versions):

  1. Launch Visual Studio
  2. Tools > Macros > Macros IDE...
  3. Right-click MyMacros > Add > Add New Item...
  4. Select Module
  5. Enter "ConvertSpacesToTabs" without quotes in the Name field
  6. Click Add
  7. Replace the contents of the new module with the code above
  8. Click Save
  9. Close the Macros IDE
  10. Tools > Macros > Macro Explorer
  11. Expand MyMacros > ConvertSpacesToTabs
  12. Double-click on FormatSolution
  13. Wait for the macro to finish

Edit
I updated the code to also support *.h, *.cpp, and *.hpp files using code from Siegmund Frenzel here: https://stackoverflow.com/a/14766393/90287


as far as I know what "Tabify" does is this - it only replaces " " (4 spaces) with a tab, it does not change the formatting or anything else.

Although I would suggest using document formatting, the "tabification" could easily be done via a custom application which would mimic the same action on all the files that you want.

Hope this helps!


For vs2010, you can use the following find and replace (this example is for tabs to 4 spaces).

In the find box, enter: ^{ *} (^{ space *} tab)

In the replace box, enter \1 (\1 space space space space)

Check the condition box and set to regular expressions. Newer versions of vs use different regular expression syntax, but the same should be doable.

Update This worked by executing once for vb files, but required multiple passes for a resx file, so you may have to execute multiple times depending on the file type...


There's a new way using the dotnet CLI:

  1. Install dotnet format by running the following command:
    dotnet tool install -g dotnet-format
  2. Run it, replacing SolutionFile.sln with the path to your solution file, with the following command line:
    dotnet format SolutionFile.sln

The indent_style of .editorconfig will be used to determine if the code will use tabs or spaces.


Macros have been removed from Visual Studio 2013 onwards (and the new version of Macros uses JavaScript rather than VBScript), so to get Rami A.'s answer to work in Visual Studio 2019:

  1. Download and install the Visual Commander extension
  2. Extensions > VCmd > Edit macro
  3. Name it
  4. Paste the following code. I have had to make some changes to it to make the code work with Visual Commander. I have also changed the file extensions that it tabifies to .cs, .aspx and .ascx so change these if you need C++/other file extensions.
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Imports Microsoft.VisualStudio.Shell
Imports VisualCommanderExt

Public Class ConvertTabsToSpaces
    Implements ICommand
    Sub Run(DTE As DTE2, package As Package) Implements ICommand.Run
        Dim sol As Solution = dte.Solution
        For i As Integer = 1 To sol.Projects.Count
            FormatProject(sol.Projects.Item(i))
        Next
    End Sub

    Private Sub FormatProject(ByVal proj As Project)
        If Not proj.ProjectItems Is Nothing Then
            For i As Integer = 1 To proj.ProjectItems.Count
                FormatProjectItem(proj.ProjectItems.Item(i))
            Next
        End If
    End Sub

    Private Sub FormatProjectItem(ByVal projectItem As ProjectItem)
        If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then
            If (projectItem.Name.LastIndexOf(".cs") = projectItem.Name.Length - 3 OrElse (projectItem.Name.LastIndexOf(".aspx") = projectItem.Name.Length - 5 OrElse (projectItem.Name.LastIndexOf(".ascx") = projectItem.Name.Length - 5))) Then
                Dim window As Window = projectItem.Open(Constants.vsViewKindCode)
                window.Activate()

                Try
                    projectItem.Document.DTE.ExecuteCommand("Edit.RemoveAndSort")
                Catch
                    ' Do nothing
                End Try

                Try
                    projectItem.Document.DTE.ExecuteCommand("Edit.SelectAll")
                    projectItem.Document.DTE.ExecuteCommand("Edit.FormatSelection")
                Catch
                    ' Do nothing
                End Try

                window.Close(vsSaveChanges.vsSaveChangesYes)
            End If
        End If

        'Be sure to format all of the ProjectItems
        If Not projectItem.ProjectItems Is Nothing Then
            For i As Integer = 1 To projectItem.ProjectItems.Count
                FormatProjectItem(projectItem.ProjectItems.Item(i))
            Next
        End If

        'Format the SubProject if it exists
        If Not projectItem.SubProject Is Nothing Then
            FormatProject(projectItem.SubProject)
        End If
    End Sub

End Class
  1. Save
  2. Run
  3. To save for future use: Extensions > VCmd > Save macro as command > Name it > Save
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜