如何修复整个 VS 解决方案不一致的行结尾?
打开文件时,Visual Studio 将检测到不一致的行结尾,并且有一个选项可以针对该特定文件修复它。但是,如果我想修复解决方案中所有文件的行结尾,该怎么做?
Visual Studio will detect inconsistent line endings when opening a file and there is an option to fix it for that specific file. However, if I want to fix line endings for all files in a solution, how do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只是为了获得更完整的答案,这对我来说最有效:
替换
在整个解决方案中
为“regEx”选项。
这将在迄今为止没有正确行结尾的所有文件中设置正确的行结尾。它使用负前视来检查 \n 前面是否不存在 \r。
请小心其他解决方案:它们将修改所有文件中的所有行(忽略原始行结尾)或删除每行的最后一个字符。
Just for a more complete answer, this worked best for me:
Replace
with
in entire solution with "regEx" option.
This will set the correct line ending in all files which didn't have the correct line ending so far. It uses the negative lookahead to check for the non-existance of a \r in front of the \n.
Be careful with the other solutions: They will either modify all lines in all files (ignoring the original line ending) or delete the last character of each line.
您可以使用“在文件中替换”命令并启用正则表达式。例如,要将具有单个换行符“\n”的行尾(例如,来自 GitHub)替换为标准 Windows 回车换行符“\r\n”,请搜索:
这表示创建一个组(这就是需要括号的原因),其中第一个字符不是回车符或者是行的开头。行测试的开头实际上仅针对文件的开头,如果它恰好以“\n”开头。该组后面是换行符。因此,您将匹配具有错误行尾的“;\n”,但不匹配正确行尾的“\r\n”。
并将其替换为:
这表示保留组 ($1),然后将“\n”替换为“\r\n”。
You can use the Replace in Files command and enable regular expressions. For example, to replace end-of-lines that have a single linefeed "\n" (like, from GitHub, for example) with the standard Windows carriage-return linefeed "\r\n", search for:
This says to create a group (that's why the parentheses are required), where the first character is either not a carriage-return or is the beginning of a line. The beginning of the line test is really only for the very beginning of the file, if it happens to start with a "\n". Following the group is a newline. So, you will match ";\n" which has the wrong end-of-line, but not "\r\n" which is the correct end-of-line.
And replace it with:
This says to keep the group ($1) and then replace the "\n" with "\r\n".
尝试执行
然后保存文档,只要文件没有被其他外部编辑器修改,它就应该保持一致。为我解决它。
Try doing
Then save the document, as long as the file doesn't get modified by another external editor, it should stay consistent. Fixes it for me.
如果您有安装了 cygutils 软件包的 Cygwin,则可以从 Cygwin shell 使用以下命令链:
unix2dos -idu *.cpp | sed -e 's/ 0 [1-9][0-9]//' -e 's/ [1-9][0-9]* 0 //' | sed '/ [1-9][0-9] / !d' | sed -e 's/ [1-9][0-9] //' | xargs unix2dos
(将 *.cpp 替换为您需要的任何通配符)
要了解其工作原理,unix2dos 命令用于转换文件,但仅限于行结尾不一致的文件(即混合文件) UNIX 和 DOS)需要转换。 -idu 选项显示文件中 dos 和 unix 行结尾的数量。例如:
这里只需要转换Acad2S5kXhat.cpp文件。 sed 命令过滤输出以生成仅需要转换的文件的列表,然后通过 xargs 处理这些文件。
If you have Cygwin with the cygutils package installed, you can use this chain of commands from the Cygwin shell:
unix2dos -idu *.cpp | sed -e 's/ 0 [1-9][0-9]//' -e 's/ [1-9][0-9]* 0 //' | sed '/ [1-9][0-9] / !d' | sed -e 's/ [1-9][0-9 ] //' | xargs unix2dos
(Replace the *.cpp with whatever wildcard you need)
To understand how this works, the unix2dos command is used to convert the files, but only files that have inconsistent line endings (i.e., a mixture of UNIX and DOS) need to be converted. The -idu option displays the number of dos and unix line endings in the file. For example:
Here, only the Acad2S5kXhat.cpp file needs to be converted. The sed commands filter the output to produce a list of just the files that need to be converted, and these are then processed via xargs.
由于尚未提及这一点,因此可以在此处找到转换当前文件中的行结尾的选项:
Since this wasn't mentioned yet, the option to convert line endings in the current file can be found here: