I'd like to know the difference (with examples if possible) between
CR LF
(Windows), LF
(Unix) and CR
(Macintosh) line break types.
我想知道 CR LF
(Windows)、LF
(Unix) 和 CR
(Macintosh) 换行类型之间的区别(如果可能的话,请举例说明)。
10 Answers 10 回答
CR and LF are control characters, respectively coded 0x0D
(13 decimal) and 0x0A
(10 decimal).
CR 和 LF 是控制字符,分别编码为 0x0D
(十进制 13 位)和 0x0A
(十进制 10 位)。
They are used to mark a line break in a text file. As you indicated, Windows uses two characters the CR LF sequence; Unix (and macOS starting with Mac OS X 10.0) only uses LF; and the classic Mac OS (before 10.0) used CR.
它们用于在文本文件中标记换行符。如你所指示的,Windows 使用两个字符 CR LF 序列;Unix(以及 Mac OS X 10.0 以上的 macOS)仅使用 LF;而经典的 Mac OS(10.0 之前)使用 CR。
An apocryphal historical perspective:
杜撰的历史观点:
As indicated by Peter, CR = Carriage Return and LF = Line Feed, two expressions have their roots in the old typewriters / TTY. LF moved the paper up (but kept the horizontal position identical) and CR brought back the "carriage" so that the next character typed would be at the leftmost position on the paper (but on the same line).
正如 Peter 所指出的,CR = 回车和 LF = 换行,两个表达式都源于旧的打字机 / TTY。LF 将纸张向上移动(但保持水平位置相同),CR 恢复“托架”,以便键入的下一个字符位于纸张的最左侧位置(但在同一行上)。
CR+LF was doing both, i.e., preparing to type a new line.
CR+LF 两者都在做,即准备输入一条新行。
As time went by the physical semantics of the codes were not applicable, and as memory and floppy disk space were at a premium, some OS designers decided to only use one of the characters, they just didn't communicate very well with one another ;-)
随着时间的推移,代码的物理语义不再适用,并且由于内存和软盘空间非常宝贵,一些操作系统设计人员决定只使用其中一个字符,他们只是不能很好地相互交流 ;-)
Most modern text editors and text-oriented applications offer options/settings, etc. that allow the automatic detection of the file's end-of-line convention and to display it accordingly.
大多数现代文本编辑器和面向文本的应用程序都提供了选项/设置等,这些选项/设置允许自动检测文件的行尾约定并相应地显示它。
-
112so actually Windows is the only OS that uses these characters properly, Carriage Return, followed by a Line Feed.
所以实际上 Windows 是唯一正确使用这些字符的操作系统,Carriage Return,然后是换行符。 Commented Sep 3, 2018 at 10:38
9月 3, 2018 在 10:38 -
13Would it be accurate, then, to say that a text file created on Windows is the most compatible of the three i.e. the most likely to display on all three OS subsets?
那么,说在 Windows 上创建的文本文件是这三个子集中最兼容的,即最有可能在所有三个操作系统子集上显示,这是否准确?– Hashim Aziz – 哈希姆·阿齐兹Commented Sep 19, 2018 at 19:12
9月 19, 2018 在 19:12 -
11@Hashim it might display properly but trying to run a textual shell script with carriage returns will usually result in an error
@Hashim它可能显示正确,但尝试运行带有回车符的文本 shell 脚本通常会导致错误 Commented Apr 24, 2019 at 14:06
4月 24, 2019 在 14:06 -
43Rolf - that statement assumes that keeping old terminology/technology in new technology is correct. CRLF = 2 bytes. CR = 1, LF = 1. With as often as they are used, that actually translates to a huge amount of data.
Rolf - 这种说法假设将旧术语/技术保留在新技术中是正确的。CRLF = 2 字节。CR = 1,LF = 1。只要他们经常使用,这实际上就会转化为大量的数据。
Once again, Windows has chosen to be different from the entirety of the *NIX world.
Windows 再一次选择与整个 *NIX 世界不同。– Phillip Boushy – 菲利普·布希Commented Apr 20, 2021 at 14:47
4月 20, 2021 在 14:47 -
13@QuaternionsRock The short is answer is because CR takes a long time on a teleprinter. Putting CR before LF gives the carriage time to get back to the other side. Sometimes even with CRLF you'd have to send NULs to give it more time.
@QuaternionsRock 简短的答案是因为 CR 在电传打印机上需要很长时间。将 CR 放在 LF 之前,可以让托架有时间回到另一侧。有时,即使使用 CRLF,您也必须发送 NUL 以给它更多时间。 Commented Oct 15, 2022 at 5:12
10月 15, 2022 在 5:12
This is a good summary I found:
这是我找到的一个很好的总结:
The Carriage Return (CR) character (0x0D
, \r
) moves the cursor to the beginning of the line without advancing to the next line. This character is used as a new line character in Commodore and early Macintosh operating systems (Mac OS 9 and earlier).
回车 (CR) 字符 (0x0D
, \r
) 将光标移动到行首,而不前进到下一行。此字符在 Commodore 和早期的 Macintosh 操作系统(Mac OS 9 及更早版本)中用作换行符。
The Line Feed (LF) character (0x0A
, \n
) moves the cursor down to the next line without returning to the beginning of the line. This character is used as a new line character in Unix-based systems (Linux, Mac OS X, etc.)
换行符 (LF) 字符 (0x0A
, \n
) 将光标向下移动到下一行,而不返回到行首。此字符在基于 Unix 的系统(Linux、Mac OS X 等)中用作换行符。
The End of Line (EOL) sequence (0x0D 0x0A
, \r\n
) is actually two ASCII characters, 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 a new line character in most other non-Unix operating systems including Microsoft Windows, Symbian and others.
行尾 (EOL) 序列 (0x0D 0x0A
, \r\n
) 实际上是两个 ASCII 字符,即 CR 和 LF 字符的组合。它将光标向下移动到下一行和该行的开头。此字符在大多数其他非 Unix 操作系统(包括 Microsoft Windows、Symbian 等)中用作换行符。
-
3The "vertical tab"-character moves the cursor down and keep the position in the line, not the LF-character. The LF is EOL.
“vertical tab(垂直制表符)”字符将光标向下移动并保持在该行中的位置,而不是 LF 字符。LF 为 EOL。 Commented Nov 17, 2016 at 12:07
11月 17, 2016 在 12:07 -
7@TaylorLeese Are /r/n and /n/r same?
@TaylorLeese /r/n 和 /n/r 是一样的吗? Commented Sep 22, 2018 at 15:54
9月 22, 2018 在 15:54 -
3Thanks for highlighting: Classic MacOS:
CR
=\r
, Unix and MacOS:LF
=\n
, Windows:CRLF
=\r\n
.
感谢您突出显示:经典 MacOS:CR
=\r,Unix
和 MacOS:LF
=\n,Windows
:CRLF
=\r\n
。– fallincode – 法林代码Commented May 19, 2022 at 11:02
5月 19, 2022 在 11:02 -
@Vicrobot Developers will often split a string or perform other operations with the exact sequence
\r\n
, so\n\r
would not match. Also one would think that text editors also treat the two characters as one sequence and don't separatly go "oh, now I have to go down one line" and "oh, now I have to move to the front". Were it so then yes, you could freely swap the order around
@Vicrobot 开发人员通常会使用精确的序列\r\n
拆分字符串或执行其他操作,因此\n\r
不会匹配。此外,有人会认为文本编辑器也将这两个字符视为一个序列,并且不会分离地说“哦,现在我必须走一行”和“哦,现在我必须移到前面”。如果是这样,那么是的,您可以自由交换订单– LukasKroess – 卢卡斯·克罗斯Commented Aug 2, 2022 at 8:50
8月 2, 2022 在 8:50
It's really just about which bytes are stored in a file. CR
is a bytecode for carriage return (from the days of typewriters) and LF
similarly, for line feed. It just refers to the bytes that are placed as end-of-line markers.
这实际上只是关于文件中存储了哪些字节。CR
是回车(来自打字机时代)的字节
码,LF 同样是换行的字节码。它仅引用作为行尾标记放置的字节。
There is way more information, as always, on Wikipedia.
一如既往,维基百科上有更多的信息。
-
126I think it's also useful to mention that
CR
is the escape character\r
andLF
is the escape character\n
. In addition, Wikipedia:Newline.
我认为提到CR
是转义字符\r,LF
是转义字符\n
也很有用。此外,Wikipedia:Newline。 Commented Sep 3, 2018 at 15:20
9月 3, 2018 在 15:20 -
1In Simple words
CR and LF
is just end of line and new line according to this link , is this correct ?
简单来说,CR 和 LF
就是根据这个链接的行尾和新行,这是正确的吗? Commented Sep 18, 2019 at 11:02
9月 18, 2019 在 11:02 -
2@shaijut CR stands for Carriage Return. That was what returned the carriage on typewriters. So, mostly correct.
@shaijut CR 代表回车。这就是打字机上的马车返回的原因。所以,大部分是正确的。 Commented May 2, 2020 at 10:50
5月 2, 2020 在 10:50 -
6The superior LFCR option is sadly missing. Its benefit is that by doing the Line Feed first, the Selectric golfball can't smear the just printed line with still fresh ink upon executing the Carriage Return </s>
遗憾的是,缺少高级 LFCR 选项。它的好处是,通过先执行换行,Selectric 高尔夫球在执行回车 </s> 时无法用仍然新鲜的墨水涂抹刚刚打印的线条 Commented Mar 25, 2021 at 10:55
3月 25, 2021 在 10:55 -
1Actually, it's not a typewriter but "teletype", old computer client terminals with mechanical print heads and paper, where CR/LF were required for computers to behave properly. If you just did CR, you would have a bunch of characters on top of each other on a paper.
实际上,它不是打字机,而是“电传打字机”,带有机械打印头和纸张的旧计算机客户端终端,其中需要 CR/LF 才能使计算机正常运行。 如果你只做 CR,你会在一张纸上有一堆字符彼此重叠。
If you just did LF, your text lines would slowly migrate to the right on the paper. CR/LF were required for proper teletype based computing.
如果您只执行 LF,您的文本行会慢慢地在纸张上向右迁移。 CR/LF 是正确的基于电传打字的计算所必需的。
An old Star Trek game would dump "F<LF><BEL><BEL>I<LF><BEL><BEL>R<LF><BEL><BEL>I<LF><BEL><BEL>N<LF><BEL><BEL>G<LF><BEL><BEL>" diagonally down the page.
一个老的星际迷航游戏会把“F<LF><BEL<BEL>I<LF><BEL>R<LF><BEL><BEL>I<LF><BEL><BEL>N<LF><BEL><BEL>G<LF><BEL><BEL>”放在页面的对角线下方。 Commented Apr 29, 2022 at 14:46
4月 29, 2022 在 14:46
Summarized succinctly: 简要总结:
Carriage Return (Mac pre-OS X)
回车(Mac OS X 之前的版本)
- CR
\r
- ASCII code 13 ASCII 码 13
Line Feed (Linux, Mac OS X)
换行符 (Linux, Mac OS X)
- LF
\n
- ASCII code 10 ASCII 码 10
Carriage Return and Line Feed (Windows)
回车和换行符 (Windows)
- CRLF
\r\n
- ASCII code 13 and then ASCII code 10
ASCII 码 13,然后是 ASCII 码 10
If you see ASCII code in a strange format, they are merely the number 13 and 10 in a different radix/base, usually base 8 (octal) or base 16 (hexadecimal).
如果你看到格式奇怪的 ASCII 码,它们只是不同基数/基数的数字 13 和 10,通常是基数 8(八进制)或基数 16(十六进制)。
-
The
\r
and\n
only works in some programming languages, although it seems to be universal among programming languages that use backslash to indicate special characters.\r
和\n
仅适用于某些编程语言,尽管它似乎在使用反斜杠表示特殊字符的编程语言中是通用的。 Commented Feb 11, 2021 at 16:17
2月 11, 2021 在 16:17 -
@Flimm yes, backslash is the commonly designated character to "escape" what follows it.
@Flimm 是的,反斜杠是通常指定的字符,用于 “转义” 它后面的内容。 Commented Feb 13, 2021 at 5:36
2月 13, 2021 在 5:36 -
1And RISCOS uses \n\r RISCOS 使用 \n\r Commented Nov 29, 2023 at 10:29
11月 29, 2023 在 10:29
Jeff Atwood has a blog post about this: The Great Newline Schism
Jeff Atwood 有一篇关于这个问题的博文:The Great Newline Schism
Here is the essence from Wikipedia:
以下是维基百科的精髓:
The sequence CR+LF was in common use on many early computer systems that had adopted teletype machines, typically an ASR33, as a console device, because this sequence was required to position those printers at the start of a new line.
序列 CR+LF 是常用的 在许多早期的计算机系统上, 采用了电传打字机, 通常为 ASR33,作为控制台 device,因为这个序列是 需要将这些打印机放置在 新行的开头。
On these systems, text was often routinely composed to be compatible with these printers, since the concept of device drivers hiding such hardware details from the application was not yet well developed; applications had to talk directly to the teletype machine and follow its conventions.
在这些 系统,文本通常是例行公事 组合以兼容这些 打印机,自设备的概念 隐藏此类硬件详细信息的驱动程序 从申请中还不是很好 发达;应用程序必须对话 直接到电传打字机,以及 遵循其惯例。
The separation of the two functions concealed the fact that the print head could not return from the far right to the beginning of the next line in one-character time.
分离 的两个函数中隐藏了 打印头不能 从最右侧返回到 中下一行的开头 单字符时间。
That is why the sequence was always sent with the CR first. In fact, it was often necessary to send extra characters (extraneous CRs or NULs, which are ignored) to give the print head time to move to the left margin. Even after teletypes were replaced by computer terminals with higher baud rates, many operating systems still supported automatic sending of these fill characters, for compatibility with cheaper terminals that required multiple character times to scroll the display.
这就是为什么 序列始终与 CR 一起发送 第一。事实上,这通常是必要的 要发送额外的字符(无关的 CR 或 NUL,它们将被忽略)设置为 给打印头移动的时间 左边距。即使在电传打字机被具有更高波特率的计算机终端取代后,许多操作系统仍然支持自动发送这些填充字符,以便与需要多个字符时间来滚动显示的更便宜的终端兼容。
-
6+1 It is by this simple understanding that I always remember in what order the combination comes. Even today we can still see this mechanical logic in any inktjet-printer (I love to understand since I hate to learn). My other memory-tricks are: "mac?
+1 正是通过这种简单的理解,我总是记得组合的顺序。即使在今天,我们仍然可以在任何喷墨打印机中看到这种机械逻辑(我喜欢理解,因为我讨厌学习)。 我的其他记忆技巧是:“mac?
Return to sender" and "NewLineFeed" (to remember that NL===LF and to remember the \n , since CR already has the R in it's abbreviation)
Return to sender“ 和 ”NewLineFeed“ (记住 NL===LF 并记住 \n ,因为 CR 的缩写中已经有 R) Commented Feb 1, 2013 at 19:52
2月 1, 2013 在 19:52 -
3"I'm dubious ... two control codes was necessary for timing". That's not what it says. It says that the extra CRs and NULs are here for giving time for it to come back, not the original CR LF.
“我很怀疑......两个控制代码对于计时是必要的”。这不是它所说的。它说额外的 CR 和 NUL 是为了给它时间回来,而不是原来的 CR LF。 Commented Sep 24, 2014 at 5:58
9月 24, 2014 在 5:58 -
16@Adrian Will you take persona experience? 1) In my old teletype days, the printer we used required
<CR><CR><LF>
- so of course I experimented with just one<CR>
. I sent<CR><LF>A
after a long line, and you could hear theA
being printed before the carriage fully returned.
@Adrian 你会接受 persona 体验吗?1) 在我以前的电传打字机时代,我们使用的打印机需要<CR><CR><LF>
- 所以我当然只试验了一个<CR>
。我排了很长的队后发送了<CR><LF>A
,您可以在车厢完全返回之前听到A
被打印出来。– John Burger – 约翰·伯格Commented Jun 11, 2016 at 17:02
6月 11, 2016 在 17:02 -
16@Adrian 2) Don't forget, this was in the electro-mechanical era, where each character did exactly one function. We often emphasised a word by printing the line, then sending
<CR><CR>
and typing the correct number of spaces, then re-printing the same word: a primitive form of bolding.
@Adrian 2) 别忘了,这是在机电时代,每个角色都只做一个功能。我们经常通过打印线条来强调一个单词,然后发送<CR><CR>
并输入正确的空格数,然后重新打印相同的单词:一种原始的粗体形式。– John Burger – 约翰·伯格Commented Jun 11, 2016 at 17:02
6月 11, 2016 在 17:02 -
5@Adrian 3) And finally, this was using Baudot (or Murray code), not ASCII. Five data bits, between one start bit and one-and-a-half stop bits. How can you have half a bit?
@Adrian 3) 最后,这是使用 Baudot(或 Murray 代码),而不是 ASCII。5 个数据位,介于 1 个 STAR 位和 1.5 个停止位之间。你怎么能有半点呢?
By waiting half a bit time before starting to send the next character, to give the print head time to return to center.
在开始发送下一个字符之前等待半点时间,让打印头有时间返回到中心。– John Burger – 约翰·伯格Commented Jun 11, 2016 at 17:06
6月 11, 2016 在 17:06
CR - ASCII code 13
CR - ASCII 码 13
LF - ASCII code 10.
LF - ASCII 码 10.
Theoretically, CR returns the cursor to the first position (on the left). LF feeds one line, moving the cursor one line down. This is how in the old days you controlled printers and text-mode monitors.
理论上,CR 将光标返回到第一个位置(左侧)。LF 馈送一行,将光标向下移动一行。这就是过去控制打印机和文本模式监视器的方式。
These characters are usually used to mark end of lines in text files.
Different operating systems used different conventions. As you pointed out, Windows uses the CR/LF combination while pre-OS X Macs use just CR and so on.
这些字符通常用于标记文本文件中的行尾。不同的操作系统使用不同的约定。正如您所指出的,Windows 使用 CR/LF 组合,而 OS X 之前的 Mac 仅使用 CR 等。
CR and LF are a special set of characters that help us format our code.
CR 和 LF 是一组特殊的字符,可帮助我们格式化代码。
CR (\r) stands for CARRIAGE RETURN. It puts the cursor at the beginning of a line, but it doesn't create a new line. This is how classic Mac OS works (not applicable today unless you are dealing with old files).
CR (\r) 代表 CARRIAGE RETURN。它将光标放在行的开头,但不会创建新行。这就是经典 Mac OS 的工作原理(除非您正在处理旧文件,否则今天不适用)。LF (\n) stands for LINE FEED. It creates a new line, but it doesn't put the cursor at the beginning of that line. The cursor stays back at the end of the last line. This is how Unix (including macOS) and Linux work.
LF (\n) 代表 LINE FEED。它会创建一个新行,但不会将光标放在该行的开头。光标将回到最后一行的末尾。这就是 Unix(包括 macOS)和 Linux 的工作原理。CRLF (\r\n) creates a new line as well as puts the cursor at the beginning of the new line. This is how we see it in Windows OS.
CRLF (\r\n) 创建新行,并将光标置于新行的开头。这就是我们在 Windows 操作系统中看到它的方式。
Git uses LF by default. So when we use Git on Windows it throws a warning like "CRLF will be replaced by LF" and automatically converts all CRLF into LF, so that code becomes compatible.
Git 默认使用 LF。因此,当我们在 Windows 上使用 Git 时,它会抛出类似 “CRLF will be replaced by LF” 的警告,并自动将所有 CRLF 转换为 LF,以便代码变得兼容。
NB: Don't worry...see this less as a warning and more as a notice thing.
NB:别担心......不要将此视为警告,而应将其视为通知。
-
2Re "This is how MAC OS works": Only the old Macs (Classic Mac OS).
关于“这就是 MAC OS 的工作原理”:仅限旧 Mac(经典 Mac OS)。 Commented Feb 10, 2023 at 13:47
2月 10, 2023 在 13:47
The sad state of "record separators" or "line terminators" is a legacy of the dark ages of computing.
“记录分隔符”或“行终止符”的悲惨状态是计算黑暗时代的遗留问题。
Now, we take it for granted that anything we want to represent is in some way structured data and conforms to various abstractions that define lines, files, protocols, messages, markup, whatever.
现在,我们理所当然地认为,我们想要表示的任何东西在某种程度上都是结构化数据,并且符合定义行、文件、协议、消息、标记等各种抽象。
But once upon a time this wasn't exactly true. Applications built-in control characters and device-specific processing. The brain-dead systems that required both CR and LF simply had no abstraction for record separators or line terminators.
但曾几何时,这并不完全正确。应用程序内置控制字符和设备特定的处理。需要 CR 和 LF 的脑死亡系统根本没有记录分隔符或行终止符的抽象。
The CR was necessary in order to get the teletype or video display to return to column one and the LF (today, NL, same code) was necessary to get it to advance to the next line. I guess the idea of doing something other than dumping the raw data to the device was too complex.
CR 是必要的,以便让电传打字机或视频显示返回到第一列,而 LF(今天,NL,相同的代码)是使其前进到下一行所必需的。我想除了将原始数据转储到设备之外,做其他事情的想法太复杂了。
Unix and Mac actually specified an abstraction for the line end, imagine that. Sadly, they specified different ones. (Unix, ahem, came first.) And naturally, they used a control code that was already "close" to S.O.P.
Unix 和 Mac 实际上为行尾指定了一个抽象,想象一下。遗憾的是,他们指定了不同的 VPN。(Unix,咳咳,先来了。自然而然地,他们使用了已经“接近”SOP 的控制代码。
Since almost all of our operating software today is a descendent of Unix, Mac, or Microsoft operating software, we are stuck with the line ending confusion.
由于我们今天几乎所有的操作软件都是 Unix、Mac 或 Microsoft 操作软件的后代,因此我们陷入了结尾混淆的困境。
Systems based on ASCII or a compatible character set use either LF (Line feed, 0x0A, 10 in decimal) or CR (Carriage return, 0x0D, 13 in decimal) individually, or CR followed by LF (CR+LF, 0x0D 0x0A); These characters are based on printer commands: The line feed indicated that one line of paper should feed out of the printer, and a carriage return indicated that the printer carriage should return to the beginning of the current line.
基于 ASCII 或兼容字符集的系统单独使用 LF(换行符,0x0A,十进制为 10)或 CR(回车,0x0D,十进制为 13),或者 CR 后跟 LF(CR+LF,0x0D 0x0A);这些字符基于打印机命令:换行指示应从打印机送出一行纸张,回车指示打印机回车应返回到当前行的开头。
NL is derived from EBCDIC NL = 0x15 which would logically compare to CRLF 0x0D 0x0A ASCII... This becomes evident when physically moving data from mainframes to midrange. Colloquially (as only arcane folks use EBCDIC), NL has been equated with either CR or LF or CRLF.
NL 源自 EBCDIC NL = 0x15,这在逻辑上可以与 CRLF 0x0D 0x0A ASCII 进行比较......在将数据从大型机物理移动到中端时,这一点变得很明显。通俗地说(因为只有神秘的人使用 EBCDIC),NL 等同于 CR 或 LF 或 CRLF。
Not the answer you're looking for? Browse other questions tagged
不是您要找的答案?浏览其他标记的问题 or ask your own question.
或者提出你自己的问题。
不是您要找的答案?浏览其他标记的问题 or ask your own question.
或者提出你自己的问题。
\n
is typically represented by a linefeed, but it's not necessarily a linefeed.非常相似,但不是完全重复的。
\n
通常由换行符表示,但不一定是换行符。3月 2, 2012 在 0:00
\r
and\n
are abstractions used in certain programming languages. Closing this question glosses over fundamental differences between the questions and perpetuates misinformation.CR 和 LF 是 ASCII 和 Unicode 控制字符,而
\r
和\n
是某些编程语言中使用的抽象。结束这个问题掩盖了问题之间的根本差异,并使错误信息永久化。10月 8, 2012 在 20:07
@AdrianMcCarthy 在某种程度上,票数接近作为答案的方式存在问题;声称两者相同的答案可以被 downvote,然后显示为非常非常错误,但只需要 4 次同意投票(类似于 Upvotes)就会发生非常错误的结束,直到它发生之前没有办法反驳投票。
3月 13, 2014 在 17:03
诚然,这个问题的这种表述更好,但就所有实际目的而言,它仍然是同一个问题。
3月 13, 2014 在 17:22
\n
doesn't mean the same thing in all programming languages.@JukkaK.Korpela:不,真的不是。
\n
在所有编程语言中的含义并不相同。3月 13, 2014 在 17:34
显示 2 更多评论