What is the difference between Windows and Linux line-wrapping rules

Source: Internet
Author: User
Tags linux

Before the computer appeared, there was a device called the teletype Model 33, which could play 10 characters per second. But it has a problem, that is, after a line of line change, to use 0.2 seconds, just can hit two characters. If there are new characters coming in this 0.2 seconds, the character will be lost.

So the developers figured out a way to solve the problem by adding two characters to the end after each line. One is called "carriage return", which tells the typewriter to position the print head at the left edge, and the other is called "carriage" (linefeed), telling the typewriter to move the paper down one line.

This is the "line" and "return" of the history, from their English name can also be seen in one or two.

Later, the computer was invented, and the two concepts were also on the computer. At the time, memory was expensive, and some scientists thought it would be a waste to add two characters to the end of each line. So, there was disagreement.

Unix system, the end of each line only "< newline >", that is, "\ n", Windows system, each line at the end of "< carriage return >< line >", that is, "\ r \ n", Mac system, each line at the end of "< Enter >." A direct consequence is that all text becomes a row when the files under the UNIX/MAC system are open in Windows, and Windows files open under Unix/mac, and a ^m symbol may be shown at the end of each line.

As a result, text files created under Linux will be connected to one line in Windows because Windows does not see a newline character (CRLF). Text files created under Windows may appear with one more ^m in Linux, which is ^m with CTRL + V CTRL + M, which means CR (carriage return).

Speaking of which, someone might ask, why is the text file I created in Windows normal in Linux?

For example, I create a text file under Windows A.txt, put it in my Linux, and open it with vim

You can see that the display results are normal and the end of the line does not have a ^m symbol. This is because vim, when opening a file, automatically detects line breaks, and if all line breaks for text are ^m$ (CRLF, or Windows newline tags), Vim automatically displays the text in DOS format, ignoring the ^m$ at the end of each line, so the text appears normal.

Note that the two arrows above my picture indicate the VIM editor at the bottom two flags [Noeol] and [DOS], first, explain the second flag "[DOS]", which means that vim recognizes that each line of text is ^m$, so vim automatically displays the file in DOS text format. So we see that the text display is normal.

So why sometimes windows created or edited files under Linux will appear ^m, Vim can not automatically recognize it? This is because VIM checks the text for every line break, as long as a line of newline characters is not in Windows format, then vim displays the file in the UNIX file format, and then the newline character is $, so we see a ^m symbol behind the line of text.

Here I use Cat-a to display the special symbol of the file: The file is four lines, you can see the line break is ^m$ (arrow), so vim will use the [DOS] file format to display the text. Here you can also find that the last line of the file has no newline character, this is the origin of the [NOEOL] flag in vim, the first figure, because the last line of text processed under Windows does not add line breaks, and the rule for text created under Linux is that each line has a newline character, including the last line. So vim prompts no end-of-line and tells us that this file contains lines that do not end with line breaks.

Use Wc-l to count the number of rows in this file:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.