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).
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.
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.
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.
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!
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.
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.
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):
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With