visual-studio 在整个 VS 解决方案的所有文件中替换选项卡的工具/技巧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4142828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tool/trick to replace tabs in all files of an entire VS solution
提问by Flores
My solution (30+ project) is a bit of a mess when it comes to mixing tabs and spaces, and I want to fix this with one easy step.
我的解决方案(30 多个项目)在混合制表符和空格时有点混乱,我想通过一个简单的步骤来解决这个问题。
Does anybody know a trick/tool to do this in one step for the entire solution?
有没有人知道在整个解决方案的一个步骤中执行此操作的技巧/工具?
EDIT: Not exactly what I meant. I want the documents to be nicely formatted. Just find/replace wont do.. documents will still be a mess. I want something like the Format -> Advanced -> Format Document command
编辑:不完全是我的意思。我希望文档的格式很好。只是查找/替换不会做.. 文件仍然会是一团糟。我想要类似 Format -> Advanced -> Format Document 命令的东西
回答by Dmitrii Lobanov
In case if your files contains spaces as tabs of equal width all along the solution (i.e. 4 spaces per tab), then you should use plain VS's Quick replacetool. In order to follow StyleCop's formatting rules, you should use spaces instead of tabs, so go to Quick replace(CTRL-h), select Use wildcardand then in Find whatfield you can use \tsymbol, replacing it with 4 spaces for all solution (Look infield).
如果您的文件在整个解决方案中包含空格作为等宽的制表符(即每个制表符 4 个空格),那么您应该使用普通 VSQuick replace工具。为了遵循StyleCop的格式规则,您应该使用空格而不是制表符,因此转到Quick replace(CTRL-h),选择Use wildcard然后在Find what字段中您可以使用\t符号,将其替换为所有解决方案(Look in字段)的4个空格。
Butif your solution files contains tabs made of spaces of different width, or you want to apply single-style formatting, then you definitely should use Resharper's feature to reformat code. This feature is called Code Cleanup. To apply code cleanupto whole solution or selected project, go to the solution explorer and select Cleanup codein context menu. But before launching reformatting on whole solution try and tweak it on one file, there's a lot of options in Resharper's settings.
但是,如果您的解决方案文件包含由不同宽度的空格组成的制表符,或者您想要应用单一样式格式,那么您绝对应该使用Resharper的功能来重新格式化代码。此功能称为代码清理。要应用于code cleanup整个解决方案或选定的项目,请转到解决方案资源管理器并Cleanup code在上下文菜单中选择。但是在对整个解决方案进行重新格式化之前,尝试在一个文件上对其进行调整,Resharper 的设置中有很多选项。
回答by Ken Egozi
there you go:
你去吧:
using System;
using System.IO;
class _Runner {
static void Main(string[] args) {
var root=args[0];
var filePaths = Directory.GetFiles(root, "*.cs", SearchOption.AllDirectories);
int updated = 0;
foreach (var path in filePaths) {
var content = File.ReadAllText(path);
var replaced = content.Replace(" ", "\t");
if (replaced == content) {
continue;
}
++updated;
Console.WriteLine("fixing " + path);
File.WriteAllText(path, replaced);
}
Console.WriteLine("fixed {0} files", updated);
}
}
Save is as spaces-to-tabs.cs, and run:
另存为spaces-to-tabs.cs,然后运行:
C:>c:\Windows\Microsoft.NET\Framework\v3.5\csc spaces-to-tab.cs
C:>spaces-to-tabs.exe C:\path\to\your\solution
回答by Mud
I keep a unix tool directory in my path when working in Windows, for just such emergencies. For instance, this would replace all tabs with 4 spaces, in any .cs files in the c:\myproject directory or it's subdirectories.
在 Windows 中工作时,我会在我的路径中保留一个 unix 工具目录,以应对此类紧急情况。例如,这会将 c:\myproject 目录或其子目录中的任何 .cs 文件中的所有制表符替换为 4 个空格。
find c:\myproject -name *.cs -exec sed -i -e "\"s/\t/ /g\"" {} ";"
回答by Slav
A simple and fast way is to open file in notepad++ and either replace it manually (switch on 'show all characters' and tabs will be highlighted), or replace it automatically (have to play with a 'Settings')
一种简单快捷的方法是在记事本++中打开文件并手动替换它(打开“显示所有字符”,标签将被突出显示),或者自动替换它(必须使用“设置”进行播放)
回答by CBarr
Not a perfect solution, but the extension "Fix Mixed Tabs" is a huge help https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.FixMixedTabs
不是一个完美的解决方案,但扩展“修复混合标签”是一个巨大的帮助 https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.FixMixedTabs
Basically when you open a file that has mixed tabs/spaces whitespace, it popup up a toolbar at the top of the file to allow you to normalize it
基本上,当您打开一个包含混合制表符/空格的文件时,它会在文件顶部弹出一个工具栏,让您对其进行规范化
回答by Abyx
ctrl-h, set "Look in:" to "entire solution"
ctrl-h,将“查看:”设置为“整个解决方案”
Also you can record a macro.
您也可以录制宏。

