Skip to content

Carriage return: CR, LF and CRLF explained

A carriage return moves to the start of a line and a line feed moves down one line. See where CR, LF and CRLF come from and which systems use each.

6 min read

A carriage return is the move back to the start of a line: on a typewriter the carriage slides back to the left margin, and in computing the control character CR (code 13) means the same thing. A line feed, LF (code 10), moves down one line, and the two together, CRLF, are how some systems mark the end of a line of text.

The carriage return on a typewriter

On a manual typewriter the paper sits in a carriage that steps to the left as you type. At the end of a line you push the return lever at the left end of the carriage. One push does two jobs: the first part of the stroke turns the roller to move the paper up a line, and the rest slides the carriage back to the right until it stops at the left margin. How a typewriter works follows the mechanism part by part.

The two jobs were always separate motions. Turn the roller by hand and you get a line feed with no return. Slide the carriage back without turning the roller and you can type over the same line again, which is how typists underlined words.

Electric typewriters replaced the lever with a key, usually labeled carriage return, return or power return. That key is the ancestor of the Return key on a computer. You can try both motions on the online typewriter: Enter returns the carriage and feeds a line, and Shift with Enter feeds a line without returning.

From the teleprinter to CR and LF

A teleprinter is a typewriter driven over a wire. Nobody at the receiving end can push a lever, so the sender has to transmit the motions as codes. Donald Murray's telegraph code of 1901 added control characters for exactly this, including a carriage return and a line feed. A descendant of his code became the international teleprinter standard in 1932.

Keeping the two codes separate had practical uses, according to Wikipedia's history of the newline:

  • Time. The print head could not get back to the left margin in the time it took to receive one character. Sending two codes gave the mechanism time to finish before the next printable character arrived.
  • Overprinting. A carriage return with no line feed let the machine print over the same line, for bold, underline and strikethrough.

When ASCII was written in the 1960s for teleprinters and computers, it kept both characters.

Name Abbreviation ASCII code Hex Common escape
Carriage return CR 13 0D \r
Line feed LF 10 0A \n

CR vs LF vs CRLF: how line endings differ across systems

Once text was stored in files, not printed as it arrived, a line needed only one marker. Systems chose differently, and the choices are still with us.

Line ending Characters Used by
LF \n Linux, macOS and other Unix-like systems
CRLF \r\n Windows, and earlier MS-DOS and CP/M
CR \r Classic Mac OS (before Mac OS X), Apple II, Commodore 64

Why Unix uses LF alone

Multics, a system whose development began in 1964, stored a single line feed and let a device driver turn it into whatever a particular printer needed. One character per line was simpler for programs to handle. Unix followed Multics, and Linux and macOS follow Unix.

Why Windows uses CRLF

CP/M kept the teleprinter pair. MS-DOS, released in 1981, adopted it to stay compatible with CP/M, and Windows inherited it from MS-DOS.

Why old Macs used CR

The classic Mac OS, like several home computers of its time, used the carriage return alone. Mac OS X is built on Unix and switched to LF, so only very old Mac files still have CR endings.

Internet protocols use CRLF

The early standards drafts disagreed, too: between 1963 and 1968 the ISO drafts allowed either CRLF or LF alone, while the American drafts allowed only CRLF. The internet's text protocols settled on CRLF. HTTP, SMTP (email) and FTP all require it at the end of each line, whatever the computers at each end use for their files.

What goes wrong with mixed line endings

Most modern software reads all three conventions without complaint. Problems appear at the edges.

  • Everything on one line. A file with LF endings, opened in an old Windows program that expects CRLF, appears as a single long line. Windows Notepad behaved this way until Microsoft added support for LF and CR endings in 2018.
  • A stray ^M. A file with CRLF endings, viewed with Unix tools, can show ^M at the end of every line. That is the carriage return.
  • Files that are rejected. Some compilers and configuration parsers refuse a file with the wrong endings.
  • Noisy version control. If one person's editor saves CRLF and another's saves LF, every line of a file can show as changed.

How to keep line endings consistent

  1. Find out what a file uses. Many code editors show the line ending of the open file and can convert it.
  2. Pick one convention for a shared project and write it down.
  3. Let version control enforce it. In Git, GitHub's guide suggests setting core.autocrlf to true on Windows and to input on macOS and Linux, or adding a .gitattributes file with * text=auto so the rule travels with the project.
  4. When moving files by FTP, send binary files in binary mode. Text mode translates line endings and will corrupt a file that is not text.

If you only need plain text on a page, with the lines where you put them, text to PDF accepts any of the three line endings.

The Enter key and the Return key

The Return key is named after the carriage return and sits at the right edge of the letter keys. The Enter key is a computer invention: on terminals it sent what you had typed to the computer, and it usually lives on the number pad.

Today the two mostly behave alike.

  • Windows keyboards usually label the main key Enter, often with a bent arrow, and Windows generally makes no distinction between it and the number pad key.
  • Mac keyboards label the main key return. macOS treats return and enter as equivalent in nearly all cases, but programs can tell them apart. In one well-known image editor, return starts a new line of text and enter finishes editing.
  • Laptops often have a single key that does both jobs.

The key's shape differs between keyboard standards as well. On ISO keyboards it is a tall key spanning two rows. On ANSI keyboards, the US pattern, it is a wide key on one row.

What the key inserts in a document depends on the program, not the key: a word processor starts a new paragraph, a text editor inserts the line ending of the file, and a chat box may send the message. For the other keys that kept their typewriter names, see typewriter keyboard layout and the Shift key.

Sources

Questions people ask

What is a carriage return?
On a typewriter it is the motion that brings the carriage back so that typing starts again at the left margin. In computing it is a control character, code 13, that means go back to the start of the current line. It is written CR, or \r in many programming languages.
What is the difference between a carriage return and a line feed?
A carriage return moves sideways, to the start of the line. A line feed, code 10, moves down one line without moving sideways. A typewriter's return lever does both at once, and a new line on a computer needs one or both depending on the system.
What is CRLF?
CRLF is a carriage return followed by a line feed, two characters that together end a line. Windows uses it for text files, and internet protocols such as HTTP and SMTP require it.
Which line ending do Windows, macOS and Linux use?
Windows uses CRLF. Linux, macOS and other Unix-like systems use LF alone. The classic Mac OS, before Mac OS X, used CR alone.
Is the Enter key the same as the Return key?
On most computers today they do the same thing. Historically Return came from the typewriter and starts a new line, while Enter is a computer invention that submits a command or a form. Some programs still treat the two differently.
Why does my file show ^M at the end of every line?
The file has Windows line endings and you are viewing it with a Unix tool. The ^M is the carriage return, which Unix does not treat as part of the line ending.