# 回车符

回车符（carriage return，CR）是一种控制字符，在 Unicode 中为 U+000D，在 ASCII 中为字节 0D，最初用于将打字机的字车移回行首，如今在计算中作为标记文本行结束的字符之一使用。<sup>[1](https://www.unicode.org/standard/reports/tr13/tr13-5.html)</sup> 它与换行符（line feed，LF，U+000A）一起构成 CRLF 对，用于结束 Windows 文本文件和许多互联网协议中的行；对于配置了行尾规范化的文本文件，Git 会将仓库行尾规范化为 LF，而工作区文件可能使用 CRLF。<sup>[2](https://www.rfc-editor.org/info/rfc5198/)</sup><sup> • </sup><sup>[10](https://git-scm.com/docs/gitattributes)</sup>

| 关键事实 | 详情 |
|---|---|
| 字符 | CR，Unicode 中为 U+000D，ASCII 中为 0D；在 EBCDIC 中也是 0D<sup>[1](https://www.unicode.org/standard/reports/tr13/tr13-5.html)</sup> |
| 行尾角色 | 各平台用于换行的字符之一：CR、LF、CRLF 或下一行（NEL）<sup>[1](https://www.unicode.org/standard/reports/tr13/tr13-5.html)</sup> |
| CRLF 对 | 换行由 CR 后跟 LF 表示；在 Unicode 网络交换中，CR 不应出现，除非其后跟有 LF<sup>[2](https://www.rfc-editor.org/info/rfc5198/)</sup> |
| 断行 | CR 是不可定制的断行类别：它在自身之后引起断行，但 CR 与 LF 之间除外<sup>[3](https://www.unicode.org/reports/tr14/)</sup> |
| Telnet 规则 | 在确实需要单独回车符的场合，必须使用序列 CR NUL<sup>[4](https://www.rfc-editor.org/info/rfc854/)</sup> |
| Git 处理 | `core.autocrlf` 在添加文件时将 CRLF 转换为 LF，设为 `true` 时在检出时将 LF 转换回 CRLF<sup>[5](https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration)</sup> |
| 终端用途 | 不带换行的回车符允许程序覆盖当前行，这是终端进度条的机制<sup>[6](https://code.mendhak.com/how-do-terminal-progress-bars-actually-work/)</sup> |

## 打字机中的起源

这个名字源于机械结构。在打字机上，字车承载纸张，打字时向左移动；到达行尾时，操作者将字车送回右边距，压纸卷筒将纸张推进一行。美国国会图书馆的办公设备历史将打字机视为计算机所继承的键盘和文本输入惯例的根源。<sup>[7](https://guides.loc.gov/office-history/typewriters-computers)</sup> 这两种运动——水平回车和垂直换行——对应着电传打字机和早期计算机终端所沿用的两个控制字符 CR 和 LF。<sup>[1](https://www.unicode.org/standard/reports/tr13/tr13-5.html)</sup>

## 跨平台的行尾

不同平台用回车符（CR）、换行符（LF）、CRLF 对或下一行（NEL）来表示换行，同一平台对它们的处理也可能不一致；Unicode 联盟联合创始人兼主席马克·戴维斯（Mark Davis）编写的《Unicode 换行指南》指出，当来自多种来源的文本混合在同一台机器上时，这是一个重大问题。<sup>[1](https://www.unicode.org/standard/reports/tr13/tr13-5.html)</sup> 在字符集转码时，这些字符通常被直接带入对应的 Unicode 码点，因此即使是处理纯 Unicode 的程序也必须应对这些差异。<sup>[1](https://www.unicode.org/standard/reports/tr13/tr13-5.html)</sup> NEL 本身并未在 ASCII 中定义；它在 ISO 6429 中定义为 C1 控制字符。<sup>[1](https://www.unicode.org/standard/reports/tr13/tr13-5.html)</sup>

在用于网络交换的 Unicode 文本中，RFC 5198 规定换行由 CR（U+000D）后跟 LF（U+000A）的序列表示，称为 CRLF，且 CR 不应出现，除非其后跟有 LF；唯一允许的其他上下文是 CR NUL 组合，但该文档并不推荐使用。<sup>[2](https://www.rfc-editor.org/info/rfc5198/)</sup> 断行算法将 CR 视为该字符之后的强制断行，唯一的例外是 CR LF 对，此时断行发生在 LF 之后。<sup>[3](https://www.unicode.org/reports/tr14/)</sup>

## 协议中的回车符

网络协议从电传打字机的实践中继承了 CRLF 对。Telnet 规范要求，在确实需要单独回车符的场合，必须使用序列 CR NUL，因为流中的裸 CR 会被解释为行尾约定的一部分。<sup>[4](https://www.rfc-editor.org/info/rfc854/)</sup> 这种填充的存在是为了防止单独的 CR 被接收系统误认为换行信号。

同一对字符还涉及安全层面。CRLF 注入是一类 Web 应用漏洞，攻击者将回车符和换行符输入应用程序，改变其运行方式或迷惑管理员，例如伪造头部或日志结构。<sup>[8](https://arxiv.org/abs/2303.02567)</sup>

## 版本控制中的回车符

跨平台协作使 CR 对实际工作的程序员变得可见。Windows 上的许多编辑器会悄悄将现有的 LF 风格行尾替换为 CRLF，或在用户按下回车键时插入两个字符。<sup>[5](https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration)</sup> Git 通过转换设置来解决这个问题：`core.autocrlf = true` 在 Windows 上检出代码时将 LF 行尾转换为 CRLF，在将文件添加到仓库时将 CRLF 转换回 LF；`core.autocrlf = input` 在提交时将 CRLF 转换为 LF，但检出时不进行转换，适合 Linux 和 macOS 系统。<sup>[5](https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration)</sup>

仓库本身存储 LF 行尾，工作目录的行尾在检出时选择。<sup>[5](https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration)</sup> `core.eol` 变量为标记为文本的文件设置行尾类型，可选值为 `lf`、`crlf` 和 `native`（平台原生行尾，为默认值）；如果 `core.autocrlf` 设为 `true` 或 `input`，此值将被忽略。<sup>[9](https://github.com/git/git/blob/master/Documentation/config/core.adoc)</sup> 按路径的属性会覆盖配置：`eol=crlf` 在检出时将文件的工作目录行尾转换为 CRLF，`eol=lf` 保持索引中的行尾，如果设置了 `text` 但既未配置 `core.autocrlf` 也未配置 `core.eol`，则 Windows 上默认为 `eol=crlf`，其他所有平台上默认为 `eol=lf`。<sup>[10](https://git-scm.com/docs/gitattributes/)</sup>

由于转换可能丢失信息，Git 包含一项安全检查。当 `core.safecrlf` 为 true 时，Git 验证 CRLF 转换是否可逆，例如提交文件再检出后是否得到原始工作区文件；如果在当前 `core.autocrlf` 设置下转换不可逆，Git 会拒绝该文件，若设置为 `warn` 则发出警告但继续。<sup>[9](https://github.com/git/git/blob/master/Documentation/config/core.adoc)</sup> GitHub 的文档建议在更改这些设置后重新规范化仓库，因为 Git 随后可能会报告实际未编辑的文件的更改。<sup>[11](https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings?apiVersion=2022-11-28&platform=linux)</sup>

## 覆盖当前行

不带换行的回车符将光标返回当前行的开头而不下移，这使得原地文本更新成为可能。终端进度指示器利用这一点：程序不在消息末尾换行，而是再发出一个回车符，将新的进度消息打印在前一条之上。<sup>[6](https://code.mendhak.com/how-do-terminal-progress-bars-actually-work/)</sup>

## References

1. [Unicode Technical Report #13: Unicode Newline Guidelines (Mark Davis, 1999)](https://www.unicode.org/standard/reports/tr13/tr13-5.html)
2. [RFC 5198: Unicode Format for Network Interchange, RFC Editor](https://www.rfc-editor.org/info/rfc5198/)
3. [UAX #14: Unicode Line Breaking Algorithm](https://www.unicode.org/reports/tr14/)
4. [RFC 854: Telnet Protocol Specification, RFC Editor](https://www.rfc-editor.org/info/rfc854/)
5. [Git – Customizing Git: Git Configuration, git-scm.com](https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration)
6. [How do terminal progress bars actually work?](https://code.mendhak.com/how-do-terminal-progress-bars-actually-work/)
7. [Typewriters & Computers, History of the Office and Office Equipment, Library of Congress](https://guides.loc.gov/office-history/typewriters-computers)
8. [Minimize Web Applications Vulnerabilities through the Early Detection of CRLF Injection, arXiv:2303.02567](https://arxiv.org/abs/2303.02567)
9. [Git – Documentation/config/core.adoc, git/git on GitHub](https://github.com/git/git/blob/master/Documentation/config/core.adoc)
10. [Git – gitattributes Documentation, git-scm.com](https://git-scm.com/docs/gitattributes/)
11. [Configuring Git to handle line endings, GitHub Docs](https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings?apiVersion=2022-11-28&platform=linux)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 19, 2026; Oct 15, 2026 · Edited: Sep 19, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Data formats and serialization › Text, encodings, and archives

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/carriage-return

English original: https://www.edgepedia.org/carriage-return
