What does Visual Studio mean by normalize inconsistent line endings?

后端 未结 11 1939
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 19:51

Visual Studio occasionally tells me:

The line endings in the following files are not consistent. Do you want to normalize the line endings?

相关标签:
11条回答
  • 2020-11-30 20:06

    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).

    0 讨论(0)
  • 2020-11-30 20:07

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

    0 讨论(0)
  • 2020-11-30 20:14

    Some lines end with \n.

    Some other lines end with \r\n.

    Visual Studio suggests you to make all lines end the same.

    0 讨论(0)
  • 2020-11-30 20:16

    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.

    0 讨论(0)
  • 2020-11-30 20:16

    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:

    • The Carriage Return Line Feed ("CRLF") character
    • The Line Feed ("LF") character
    • The Carriage Return ("CR") character

    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:

    touch .gitattributes
    

    And add the following in it:

    # Enforce Unix newlines
    * text=auto eol=lf
    

    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.

    git rm --cached -r .
    git reset --hard
    

    That's all.

    I hope this helps

    0 讨论(0)
提交回复
热议问题