Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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 does more than I want (I don't want to run a complete formatting, just tabifying).

like image 822
Borek Bernard Avatar asked Jun 14 '10 07:06

Borek Bernard


People also ask

How do I find and replace a tab in Visual Studio?

You can use find and replace (CTRL + H). In find field you type 4 spaces, and in replace with field type one tab; then press replace all.

How do I search for a tab in Visual Studio?

If you're in the Visual Studio editor, wanting to search for a selected word or phrase, double-tap Alt+= (or whatever hotkey you've configured for Entrian Source Search) to search in a new tab.

Is there a way to tabify a document?

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.

What does tabify actually do?

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!

How do I tabify a string in Java?

The Tabify method iterates the string, appending any TABs it encounters and removing any SPACE characters. If the caller specifies four SPACEs per indent and a string begins with [ SPACE SPACE TAB ], then the two SPACEs will be removed. A contiguous sequence of SPACEs of length equal to SpacesPerIndent will cause a TAB to be appended.

How do I untabify multiple tabs in a Word document?

Press F1 and look for the command Untabify and hit Enter or press Ctrl-T Ctrl-U. Hit enter. Type how many spaces per tab you want to use and hit Enter.


2 Answers

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

like image 44
Rami A. Avatar answered Oct 02 '22 01:10

Rami A.


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.

like image 168
Pete Stensønes Avatar answered Oct 02 '22 00:10

Pete Stensønes