CRLF vs LF: Which Line Ending to Use, and How to Convert
LF (line feed, \n) is a single character that ends a line on Linux, macOS and the web. CRLF is two characters, carriage return plus line feed (\r\n), and it ends a line on Windows. Use LF for code, scripts and files shared between systems, and keep CRLF only where a format asks for it. This guide helps you choose for each situation, explains the problems the mismatch causes, and shows how to check and convert line endings in editors, Git and the command line.
CRLF vs LF in one table
| LF | CRLF | |
|---|---|---|
| Name | Line feed, also called newline | Carriage return + line feed |
| Characters | 1 | 2 |
| Code points | U+000A | U+000D U+000A |
| Decimal and hex | 10, 0A | 13 10, 0D 0A |
| Escape | \n | \r\n |
| Used by | Linux, macOS, Android, iOS, Unix tools, most modern code repositories | Windows, HTTP headers, SMTP email, the CSV standard |
The older Mac systems up to version 9 used a lone carriage return (CR). You rarely meet it now. The history of the two characters, from typewriters to teleprinters, is in our guide to carriage return and line feed, so this page sticks to the practical side.
What is a line feed?
A line feed is the control character that moves the text position down to the next line. It is character number 10 in ASCII and Unicode. Programming languages write it as \n and call it a newline. A line feed is not the same as a form feed (U+000C), which started a new page on a printer, or a vertical tab (U+000B). The cleaner treats the last two as control codes and removes them when invisible characters are being removed, but it keeps the line feed itself.
Which one should you use?
| Situation | Use | Why |
|---|---|---|
| Source code in a shared repository | LF | Avoids whole-file diffs when people use different systems |
| Shell scripts, Makefiles, Dockerfiles | LF | Unix tools read a trailing CR as part of the last word on the line |
| Windows batch files (.bat, .cmd) | CRLF | Windows tools expect it, and labels can misbehave with LF only |
| CSV files | CRLF is the written standard; LF is accepted by most readers | Match whatever the receiving program expects |
| Text for web forms and the web | LF | HTML and JavaScript use LF inside text |
| Plain notes on your own computer | Either | Modern editors handle both |
The rule of thumb: pick one for each project, write it down in the repository, and let tools enforce it.
Problems caused by a mismatch
- A script that will not start. A Linux or macOS script saved with CRLF fails with
bad interpreterorcommand not found, because the invisible CR is read as part of the command name. - Every line shows as changed in Git. If an editor rewrites all the endings in a file, a diff lights up although no visible character changed.
- The last value of every CSV row will not match. A stray CR sticks to the end of the final field.
- Character limits count differently. One CRLF is two characters and one LF is one. A text that fits a limit in one form may not in the other, depending on how the system counts.
- Doubled line breaks. Text converted twice, or by two tools, can gain an empty line after every line.
- Text boxes on the web. A page reads a textarea's value with LF endings, and a submitted form normalizes them to CRLF. Back-end code that compares the text with a stored value should normalize first.
How to check which line endings a file has
| Tool | How |
|---|---|
| Visual Studio Code | The status bar at the bottom right shows LF or CRLF |
| Notepad on Windows | The status bar shows Windows (CRLF), Unix (LF) or Macintosh (CR) |
| Notepad++ | View > Show Symbol > Show End of Line |
| Vim | :set ff? answers unix, dos or mac |
| Command line | file notes.txt mentions CRLF line terminators when it finds them |
| Git | git ls-files --eol shows the endings in the index and in the working tree for each file |
How to convert
In an editor
In Visual Studio Code, click LF or CRLF in the status bar, choose the other one and save. The setting files.eol controls which ending new files get. In Notepad++, use Edit > EOL Conversion. In Vim, :set ff=unix or :set ff=dos, then write the file.
On the command line
dos2unix input.txt # CRLF to LF, if the tool is installed
unix2dos input.txt # LF to CRLF
tr -d '\r' < input.txt > output.txt # remove every CR
In Git
The cleanest approach is a .gitattributes file in the repository, because it applies to everyone who clones it:
* text=auto
*.sh text eol=lf
*.bat text eol=crlf
The per-machine setting core.autocrlf does a similar job. With true, Git converts LF to CRLF when checking out and back to LF when committing. With input, it converts to LF on commit and leaves checkouts alone. A shared .editorconfig with end_of_line = lf keeps editors in line too.
In Python
Text mode translates line endings for you. To read a file exactly as it is, open it with newline="". To write LF on every system, pass newline="\n" when opening the file for writing.
In a spreadsheet
=SUBSTITUTE(A1, CHAR(13), "")
A line break typed inside a cell with Alt + Enter is a line feed, character 10.
What the AI Text Cleaner does with line endings
Every CRLF pair and every lone CR in the text becomes a single LF. This always happens, there is no switch for it, and the tool cannot produce CRLF. The cleaned text therefore always has LF endings, which is what web forms and most code editors expect. The vertical tab, form feed and next-line character (U+0085) are deleted when Remove invisible characters is on. Runs of three or more line breaks are collapsed to one blank line unless you switch that off, and paragraph breaks stay. To remove line breaks completely, see how to remove line breaks.
Common mistakes
- Converting a file in one editor while a teammate's editor converts it back.
- Forcing CRLF on a file that Linux has to run.
- Searching a file for
\nin a tool that does not use regular expressions. - Fixing the symptom (
^Mon screen) and leaving the repository settings as they were.
Quick answers
What is the difference between CRLF and LF? LF is one character, used by Unix-like systems. CRLF is two characters, used by Windows.
Should I use CRLF or LF in Git? Store LF in the repository and use .gitattributes to say which files need CRLF in the working tree.
What does ^M mean? It is how many Unix tools display a carriage return, and shows that a file has Windows line endings.
Does Windows still need CRLF? Most modern Windows programs read both. Batch files and some older tools still prefer CRLF.