在 Visual Studio 中批量标准化行结尾
当您打开具有混合行结尾的文件时,Visual Studio 将提示您对其进行规范化。有没有办法规范当前解决方案中的所有文件?
不回答我的问题的相关帖子:
When you open a file with mixed line endings, Visual Studio will prompt you to normalize it. Is there a way to normalize all files in the current solution?
Related posts that don't answer my question:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如 Yellowblood 在 Aaron F. 的回答中指出的那样,如果您将
\n
(LF) 替换为\r\n
(CRLF),那么您将度过一段糟糕的时光,因为它会在每个 LF 之前添加一个 CR,即使是那些已经有的 LF。但是,您可以使用任何支持文件中批量替换的文本编辑器(例如 Notepad++ 或 Visual Studio 的“在文件中替换”)通过正则表达式来实现。
例如,要将 LF 替换为 CRLF,请确保激活正则表达式选项,然后将所有出现的 替换
为
\r
是回车符 (CR),\n
是换行(LF)。模式(? 将匹配其前一个字符不是回车符的任何换行符,而不捕获(即替换)该前一个字符。
另一种方法要简单得多:只需将
\r\n
替换为\n
即可。与往常一样,备份文件并确保在处理整个解决方案之前测试单个文件的操作。
我本来想将此添加为对 Aaron F. 的答案的评论,但我的声誉不够高:)
As yellowblood pointed out in Aaron F.'s answer, if you replace
\n
(LF) with\r\n
(CRLF), you will have a bad time since it will add a CR before every LF, even those that already had one.However, what you want can be achieved with regular expressions using any text editor that supports batch replacing in files (like Notepad++ or Visual Studio's "Replace in Files").
For example, to replace LF with CRLF, make sure to activate the regex option then replace all occurences of
with
\r
is a carriage return (CR),\n
is a line feed (LF). The pattern(?<!\r)\n
will match any line feed whose previous character is not a carriage return, without capturing (i.e. replacing) that previous character.The other way around is much simpler: simply replace
\r\n
with\n
.As always, back up your files and make sure to test the operation on a single file before processing the whole solution.
I would have added this as a comment to Aaron F.'s answer but my reputation isn't high enough :)
您可以创建一个bat 文件来递归地标准化所有文件。
请注意,您必须将文件路径更新为 todos.exe em>,可能还有 *.cs 过滤器。
然后运行bat文件并等待它完成。
You can create a bat file that normalize all files recursively.
Note that you have to update the file path to todos.exe, and possibly the *.cs filter.
Then you run the bat file and wait for it to finish.
Visual Studio 没有用于规范整个解决方案的行结尾的机制。该功能本质上仅限于检查文件在打开和更改时是否正确。
不过,行结尾规范化是由 Visual Studio 在
IEditorOperations::NormalizeLineEndings
中公开的 API。因此,可以编写一个插件/脚本来对解决方案中的项目执行操作。Visual Studio doesn't have a mechanism for normalizing line endings for an entire solution. The feature is essentially limited to checking if a file is correct upon opening and changing at that point.
The normalization of the line endings though is an API that is exposed by Visual Studio in
IEditorOperations::NormalizeLineEndings
. Hence it is possible to write a plugin / script which does the action against items in the solution.我刚刚使用Notepad++查找和替换功能:
在执行此操作之前您应该备份或提交所做的任何更改的
I just used Notepad++ find and replace feature :
you should back up or commit any changes you have before doing this