Carriage Return and Line Feed: CR, LF and CRLF vs LF Explained
Every line of text ends with a marker that you never see. On Windows that marker is two characters, a carriage return followed by a line feed. On macOS and Linux it is the line feed alone. Most of the time nobody notices, until a script will not run or every line of a file shows up as changed. This guide explains CR, LF and CRLF, where they came from, and how to see and convert them.
CR, LF and CRLF in one table
| Name | Short form | Unicode | Decimal | Hex | Escape | Caret form |
|---|---|---|---|---|---|---|
| Carriage return | CR | U+000D | 13 | 0D | \r | ^M |
| Line feed | LF | U+000A | 10 | 0A | \n | ^J |
| Both in a row | CRLF | U+000D U+000A | 13 10 | 0D 0A | \r\n | ^M^J |
Both are control characters: they have no shape of their own and exist to tell a device what to do. The line feed is often called a newline, and that is the meaning of \n in most programming languages.
Where the two characters come from
The names describe a typewriter. The carriage was the part that held the paper and slid sideways as you typed. A carriage return pushed it back so that typing started again at the left margin, and a line feed rolled the paper up by one line. A typist did both with a single lever.
Teleprinters, the machines that printed text sent over a wire, needed a separate code for each movement. Early computers used teleprinters as their terminals, so both codes ended up in the ASCII character set and in every file those computers wrote. The Enter key on some keyboards is still labelled Return for the same reason.
Later systems disagreed on how many of the two codes were really needed to end a line in a file, and the disagreement never went away.
Which systems use which
| Line ending | Used by |
|---|---|
| LF | Linux, macOS, Android, iOS, and Unix systems in general |
| CRLF | Windows and, before it, MS-DOS; also network protocols such as HTTP headers and email, and the usual definition of the CSV format |
| CR | The classic Mac OS, up to version 9; rare today |
Modern programs cope with either style when they read a file. Notepad on Windows used to show a file with LF endings as one endless line, but it has handled all three styles since an update to Windows 10 in 2018. The trouble today is mostly with tools that are strict about what they accept.
Symptoms of wrong or mixed line endings
- A shell script will not start. A script saved with CRLF endings fails on Linux or macOS with a message such as
bad interpreter: No such file or directory, usually with^Min it, or with$'\r': command not found. The invisible carriage return has become part of a command name. ^Mat the end of lines. Editors and pagers on Unix systems show a stray carriage return this way.- A diff where every line changed. If an editor rewrites all the line endings in a file, version control sees each line as modified, even though no visible character is different.
- Warnings from Git. Messages saying that LF will be replaced by CRLF, or the reverse, mean that Git is converting line endings according to its settings.
- Values that will not match. The last field of each row in a CSV file can carry a hidden carriage return, so a comparison with the expected value fails.
- Doubled or missing line breaks after text has passed through several programs that each converted the endings in their own way.
How to see which line endings a file has
- Visual Studio Code: the status bar at the bottom right shows
LForCRLFfor the current file. - Notepad on Windows: the status bar shows Windows (CRLF), Unix (LF) or Macintosh (CR).
- Notepad++: View > Show Symbol > Show End of Line draws a small CR and LF marker at each line end.
- Vim: type
:set ff?and it answersunix,dosormac. - Command line: the
filecommand mentions CRLF line terminators when it finds them, and a byte dump shows each character.
file notes.txt
od -c notes.txt | head
In the dump, a carriage return appears as \r and a line feed as \n.
How to convert between CRLF and LF
In an editor
In Visual Studio Code, click the LF or CRLF label in the status bar, choose the other one and save. In Notepad++, use Edit > EOL Conversion. In Vim, type :set ff=unix or :set ff=dos and then write the file.
On the command line
The dos2unix and unix2dos utilities exist for exactly this purpose, although you may have to install them. Without them, this removes every carriage return from a file:
tr -d '\r' < input.txt > output.txt
Perl can edit a file in place, turning CRLF into LF:
perl -pi -e 's/\r\n/\n/' notes.txt
In Git
Git can keep LF endings in the repository and convert them on each computer. The setting core.autocrlf controls this per machine. A .gitattributes file in the repository is more dependable, because it travels with the project and applies to everyone:
* text=auto
*.sh text eol=lf
*.bat text eol=crlf
The first line lets Git normalise text files. The other two force a particular ending for files that need one.
In an editor configuration
Many editors read a file named .editorconfig. The line end_of_line = lf in it tells them which ending to write for new files.
In a spreadsheet
A line break inside a cell is a line feed, character 10. To remove a stray carriage return from a cell:
=SUBSTITUTE(A1, CHAR(13), "")
Line endings in the AI Text Cleaner
When you paste text into the AI Text Cleaner, every CRLF pair and every lone carriage return becomes a single line feed. This is not an option you can switch off, and the tool has no setting for producing CRLF. The cleaned text always uses LF, which is what web forms, code editors and most online tools expect. Line endings are not shown in its list of unusual characters, because every text has them.
Rarer line-break controls are treated as control codes: with Remove invisible characters on, the vertical tab, the form feed and the next-line character U+0085 are deleted and reported in the list. If your goal is to get rid of the line breaks themselves, see how to remove line breaks.
Other characters that end a line
CR and LF are not the only ones. Unicode also defines the line separator U+2028 and the paragraph separator U+2029, and the control set includes the vertical tab U+000B, the form feed U+000C and the next-line character U+0085. They are uncommon, but when one slips into a data file, some programs treat it as a line break and others do not, which produces rows that split in one tool and not in another.
Quick answers
What is the difference between a carriage return and a line feed? They are two different control characters, numbers 13 and 10. Originally the first moved to the start of the line and the second moved down one line. Today one or both of them mark the end of a line, depending on the system.
Should I use CRLF or LF? Use LF for code, scripts and anything shared between systems, and let tools convert where needed. Keep CRLF for files that require it, such as Windows batch files.
What does ^M mean at the end of a line? It is the way many Unix tools display a carriage return. It tells you the file has Windows line endings, or a mixture.