在 Visual Studio 中批量标准化行结尾

发布于 2025-01-07 18:21:22 字数 593 浏览 1 评论 0原文

当您打开具有混合行结尾的文件时,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

背叛残局 2025-01-14 18:21:22

正如 Yellowblood 在 Aaron F. 的回答中指出的那样,如果您将 \n (LF) 替换为 \r\n (CRLF),那么您将度过一段糟糕的时光,因为它会在每个 LF 之前添加一个 CR,即使是那些已经有的 LF。

但是,您可以使用任何支持文件中批量替换的文本编辑器(例如 Notepad++ 或 Visual Studio 的“在文件中替换”)通过正则表达式来实现。

例如,要将 LF 替换为 CRLF,请确保激活正则表达式选项,然后将所有出现的 替换

(?<!\r)\n

\r\n

\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

(?<!\r)\n

with

\r\n

\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 :)

悲念泪 2025-01-14 18:21:22

您可以创建一个bat 文件来递归地标准化所有文件。

  1. 下载命令行工具 Tofrodos
  2. 在解决方案的根目录中创建一个包含以下内容的bat文件:

对于 (*.cs) 中的 /R %%i 执行 "C:\Prgs\Tofrodos\todos.exe" -p "%%i"

请注意,您必须将文件路径更新为 todos.exe em>,可能还有 *.cs 过滤器。

然后运行bat文件并等待它完成。

You can create a bat file that normalize all files recursively.

  1. Download the command line tool Tofrodos.
  2. Create a bat file in the root of you solution directory with the following content:

for /R %%i in (*.cs) do "C:\Prgs\Tofrodos\todos.exe" -p "%%i"

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.

盗梦空间 2025-01-14 18:21:22

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.

药祭#氼 2025-01-14 18:21:22

我刚刚使用Notepad++查找和替换功能:

  1. 设置NotePad++显示行结尾,查看->显示符号->显示所有字符
  2. 打开“在文件中查找”对话框
  3. 设置查找内容 \r\n(或 \n),具体取决于您正在执行的转换
  4. 设置替换为 \n(或 \r\n)
  5. 将目录指向您的解决方案 文件夹

在执行此操作之前您应该备份或提交所做的任何更改的

I just used Notepad++ find and replace feature :

  1. Set NotePad++ to show line endings, view->show symbols -> show all characters
  2. Open up the find in files dialog
  3. set find what to \r\n (or \n) depending on which coversion you're doing
  4. set replace with to \n (or \r\n)
  5. Point the Directory at your solution folder

you should back up or commit any changes you have before doing this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文