Visual Studio 标准化不一致的行结尾是什么意思?
Visual Studio 偶尔会告诉我:
以下文件中的行结尾不一致。 您想标准化行结尾吗?
然后它会向我提供不同标准或其他标准的下拉列表,例如 Windows、Mac、Unix 和一些 Unicode 标准。
这意味着什么以及如果我单击“是” 会发生什么?
Visual Studio occasionally tells me:
The line endings in the following files are not consistent. Do you want to normalize the line endings?
It then gives me a drop down with different standards or something, such as Windows, Mac, Unix, and a couple of Unicode ones.
What does this mean and what is going to happen if I click Yes
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这通常意味着您的行以回车/换行对以外的其他内容结尾。 当您从网页复制并粘贴到代码编辑器时,通常会发生这种情况。
规范化行结尾只是确保所有行结尾字符一致。 它可以防止一行以
\r\n
结尾,而另一行以\r
或\n
结尾; 第一个是 Windows 线路端对,而其他的通常用于 Mac 或 Linux 文件。由于您是在 Visual Studio 中进行开发,因此您显然需要从下拉列表中选择“Windows”。 :-)
What that usually means is that you have lines ending with something other than a carriage return/line feed pair. It often happens when you copy and paste from a web page into the code editor.
Normalizing the line endings is just making sure that all of the line ending characters are consistent. It prevents one line from ending in
\r\n
and another ending with\r
or\n
; the first is the Windows line end pair, while the others are typically used for Mac or Linux files.Since you're developing in Visual Studio, you'll obviously want to choose "Windows" from the drop down. :-)
有些行以
\n
结尾。其他一些行以
\r\n
结尾。Visual Studio 建议您使所有行的结尾相同。
Some lines end with
\n
.Some other lines end with
\r\n
.Visual Studio suggests you to make all lines end the same.
如果您使用的是 Visual Studio 2012:
转到菜单文件 → 高级保存选项 → 选择行结尾类型为Windows ( CR LF)。
If you are using Visual Studio 2012:
Go to menu File → Advanced Save Options → select Line endings type as Windows (CR LF).
要打开/关闭该选项,请从菜单栏中执行以下步骤:
工具 → 选项 → 环境 → 文档 → 检查加载时行尾是否一致
To turn the option ON/OFF, follow the steps below from menu bar:
Tools → Options → Environment → Documents → Check for consistent line endings on load
行结尾也称为换行符、行结束符(EOL)或换行符是控制字符或序列字符编码规范(例如 ASCII 或 EBCDIC)中的控制字符,用于表示一行文本的结束和新文本行的开始。 某些文本编辑器会在您按Enter 键时设置/实现此特殊字符。
回车、换行字符是行尾 (EOL) 的 ASCII 表示形式。 它们将结束字符串的当前行,并开始新的一行。
但是,在操作系统级别,它们的处理方式有所不同:
回车符(“CR”)(ASCII 13\0x0D,\r):将光标移动到开头行而不前进到下一行。 该字符在 Commodore 和早期 Macintosh 操作系统(Mac OS 9 及更早版本)中用作换行符。
换行(“LF”)字符(ASCII 10\0x0A,\n):将光标向下移动到下一行,而不返回到行首。 该字符在基于 Unix 的系统(Linux、macOS X、Android 等)中用作换行符。
回车换行(“CRLF”)字符(0x0D0A,\r\n):这实际上是两个 ASCII 字符,是 CR 和 LF 字符的组合。 它将光标向下移动到下一行和该行的开头。 该字符在大多数其他非 Unix 操作系统(包括 Microsoft Windows 和 Symbian OS)中用作换行符。
在 Visual Studio 中标准化不一致的行结尾意味着选择一种字符类型用于所有文件。 它可能是:
但是,您可以使用根目录中的 .gitattributes 文件以更好的方式进行设置,以避免将文件从一个操作系统移动到另一个操作系统时发生冲突。
只需在应用程序的根目录中创建一个名为
.gitattributes
的新文件:并在其中添加以下内容:
这会强制使用 Unix 换行 行结束字符。
注意:如果这是一个已经存在的项目,只需运行此命令即可使用
.gitattributes
中指定的新定义的行结尾来更新应用程序的文件。就这样。
我希望这有帮助
Line endings also called newline, end of line (EOL) or line break is a control character or sequence of control characters in a character encoding specification (e.g. ASCII or EBCDIC) that is used to signify the end of a line of text and the start of a new one. Some text editors set/implement this special character when you press the Enter key.
The Carriage Return, Line Feed characters are ASCII representations for the end of a line (EOL). They will end the current line of a string, and start a new one.
However, at the operating system level, they are treated differently:
The Carriage Return ("CR") character (ASCII 13\0x0D, \r): Moves the cursor to the beginning of the line without advancing to the next line. This character is used as the new line character in Commodore and Early Macintosh operating systems (Mac OS 9 and earlier).
The Line Feed ("LF") character (ASCII 10\0x0A, \n): Moves the cursor down to the next line without returning to the beginning of the line. This character is used as the new line character in Unix based systems (Linux, macOS X, Android, etc).
The Carriage Return Line Feed ("CRLF") character (0x0D0A, \r\n): This is actually two ASCII characters and is a combination of the CR and LF characters. It moves the cursor both down to the next line and to the beginning of that line. This character is used as the new line character in most other non-Unix operating systems, including Microsoft Windows and Symbian OS.
Normalizing inconsistent line endings in Visual Studio means selecting one character type to be used for all your files. It could be:
However, you can set this in a better way using
.gitattributes
file in your root directory to avoid conflicts when you move your files from one Operating system to the other.Simply create a new file called
.gitattributes
in the root directory of your application:And add the following in it:
This enforces the Unix line feed line ending character.
Note: If this is an already existing project, simply run this command to update the files for the application using the newly defined line ending as specified in the
.gitattributes
.That's all.
I hope this helps
您正在编辑的文件已使用某些不使用相同行结尾的其他编辑器进行过编辑,从而导致文件具有混合行结尾。
用于行结束的 ASCII 字符有:
CR、回车
LF、换行
窗口 = CRLF
Mac OS 9 或更早版本 = CR
Unix = LF
The file you are editing has been edited with some other editor that does not use the same line endings, resulting in a file with mixed line endings.
The ASCII characters in use for line endings are:
CR, Carriage Return
LF, Line Feed
Windows = CRLF
Mac OS 9 or earlier = CR
Unix = LF
维基百科换行文章可能会帮助您。 以下是摘录:
The Wikipedia newline article might help you out. Here is an excerpt:
当您从网络复制粘贴某些内容时,您可能会得到不一致的行结尾。
为了解决这个问题,您可以使用 Visual Studio 扩展“行尾统一器”,它可以在保存文件时自动使行尾保持一致。
When you copy paste something from web, you might get the inconsistent line endings.
In order to fix this, you can use Visual studio extension "Line Endings Unifier" which can make line ending consistent automatically while saving file.
这意味着,例如,某些文本行带有
(Windows 标准),而有些文本行仅以结尾。
(Unix 标准)。如果您单击“是”,源文件中的行尾将被转换为具有相同的格式。
这不会对编译器产生任何影响(因为行尾仅算作空白),但可能会对其他工具产生一些影响(例如版本控制系统上的“差异”)。
It means that, for example, some of your lines of text with a
<Carriage Return><Linefeed>
(the Windows standard), and some end with just a<Linefeed>
(the Unix standard).If you click 'yes' these the end-of-lines in your source file will be converted to have all the same format.
This won't make any difference to the compiler (because end-of-lines count as mere whitespace), but it might make some difference to other tools (e.g. the 'diff' on your version control system).
不仅仅是 Visual Studio...任何读取文件、编译器、链接器等的工具都必须能够处理它。
一般来说(对于软件开发)我们接受多平台行结束问题,但让版本控制软件处理它。
It's not just Visual Studio... It'd be any tools that read the files, compilers, linkers, etc. that would have to be able to handle it.
In general (for software development) we accept the multiplatform line ending issue, but let the version control software deal with it.
Visual Studio 2008 有一个加载项,可以在保存文件时转换行尾格式。 您可以在这里下载:http://grebulon.com/software/stripem.php
There'a an add-in for Visual Studio 2008 that converts the end of line format when a file is saved. You can download it here: http://grebulon.com/software/stripem.php
我在 VS 2019 中遇到了这个问题,并通过为 Windows 设置 LineBreak 配置来解决它,然后再为 Unix 设置。
工具> 选项> 保存时格式化> 设置> 换行符:Windows
I had this problem in VS 2019 and solved it by setting the LineBreak configuration for Windows, before it was set for Unix.
Tools > Options > Format on Save > Settings > LineBreak: Windows