How Line Endings Work (LF vs CRLF)
Understand how line endings work, why different operating systems use different formats, and how to manage them correctly in modern development.
Line endings are invisible control characters that indicate where one line of text ends and the next begins. Although users rarely notice them, different operating systems represent new lines differently, which can lead to formatting problems, version control conflicts and unexpected behavior in software.
Understanding LF and CRLF is especially important for developers working across Windows, macOS and Linux systems, as well as anyone managing source code, configuration files or text-based data.
What Is a Line Ending?
A line ending is a special control character—or combination of characters—that tells software where a new line begins. Unlike ordinary letters and numbers, line endings are normally invisible when viewing text.
The Two Most Common Formats
| Format | Characters |
|---|---|
| LF | Line Feed (\n) |
| CRLF | Carriage Return + Line Feed (\r\n) |
What Does LF Mean?
LF stands for Line Feed. It moves the cursor to the next line and is represented by the newline character \n. Modern Unix-based operating systems—including Linux and macOS—use LF as their standard line ending.
What Does CRLF Mean?
CRLF combines two control characters: Carriage Return (CR) followed by Line Feed (LF). Windows continues to use this two-character sequence as its default line ending.
LF:
Line 1
Line 2
CRLF:
Line 1
Line 2Why Two Different Formats Exist
The distinction dates back to mechanical typewriters and teleprinters. Returning the carriage to the beginning of a line and advancing the paper were originally separate physical operations, represented by CR and LF.
Operating System Defaults
| Operating System | Default Line Ending |
|---|---|
| Windows | CRLF |
| Linux | LF |
| macOS | LF |
Why Line Endings Matter
Different line endings usually display correctly in modern editors, but they may still affect scripts, build tools, version control systems and applications that expect a specific format.
- Git repositories.
- Shell scripts.
- Configuration files.
- Programming source code.
- CSV and text processing.
Invisible but Important
Because line endings are invisible, developers may accidentally mix formats within the same project without realizing it until problems appear.
Viewing Line Endings
Whitespace visualization tools and advanced text editors can reveal hidden line-ending characters, allowing developers to identify mixed formatting before it causes issues.
Line Endings and Git
Git can automatically convert line endings when files are checked in or out of a repository. This helps teams working on different operating systems maintain a consistent project while allowing developers to use their preferred local format.
Without proper configuration, Git may report files as modified even when only the line endings have changed.
The .gitattributes File
Many projects define line-ending behavior using a .gitattributes file. This ensures all contributors use consistent formatting regardless of their operating system.
* text=autoWhy Mixed Line Endings Cause Problems
A single file containing both LF and CRLF line endings can confuse diff tools, version control systems and text-processing utilities. It also makes collaboration more difficult because formatting changes appear alongside real code changes.
Shell Scripts
Unix shell scripts typically require LF line endings. Files saved with CRLF may produce execution errors because the extra carriage return becomes part of the interpreted text.
Programming Languages
Most programming languages ignore line-ending differences, but build tools, compilers, interpreters and testing frameworks sometimes expect consistent formatting across all project files.
CSV and Data Files
CSV, TXT and log files exchanged between operating systems may contain unexpected line endings. Data processing tools should either normalize line endings automatically or convert them before parsing.
Converting Line Endings
Line-ending converters replace every newline sequence with a consistent format. This process does not modify the visible content of the file—it only standardizes the hidden control characters.
Detecting Line Ending Problems
Whitespace visualization tools, invisible character detectors and advanced editors can reveal whether a file uses LF, CRLF or a mixture of both.
LF
LF
CRLF
LF
CRLFCommon Mistakes
- Mixing LF and CRLF in one file.
- Ignoring Git line-ending settings.
- Editing Unix scripts on Windows without conversion.
- Copying files between systems without checking formatting.
- Assuming every editor uses the same defaults.
Best Practices
Choose a consistent line-ending format for your project, configure Git appropriately, use editor settings that preserve the selected format and verify line endings before committing files to shared repositories.
Frequently Asked Questions
What is the difference between LF and CRLF?
LF (Line Feed) uses a single newline character (\n), while CRLF (Carriage Return + Line Feed) uses two characters (\r\n). Linux and modern macOS use LF by default, whereas Windows uses CRLF.
Can different line endings break my code?
In many programming languages they do not affect execution, but they can cause problems with shell scripts, version control systems, build tools and text-processing utilities that expect a consistent format.
Why does Git show changes even when I didn't edit the file?
Git may detect changes if line endings have been converted from LF to CRLF or vice versa. Proper Git configuration and a .gitattributes file help prevent unnecessary differences.
Which line ending should I use?
For most cross-platform projects, LF is the preferred choice because it is the standard on Unix-based systems and is widely supported by modern development tools. Windows editors can typically work with LF files without issues.
How can I convert line endings?
A line ending converter or most modern code editors can convert CRLF to LF or LF to CRLF without changing the visible text content.
Helpful Text Formatting Tools
A Line Ending Converter standardizes LF and CRLF formats across files, a Whitespace Visualizer reveals hidden spaces, tabs and newline characters, an Invisible Character Detector identifies formatting characters that are normally hidden, a Text Cleaner removes unwanted whitespace and formatting marks, and a UTF-8 Inspector helps verify text encoding when troubleshooting file compatibility issues.
Conclusion
Although line endings are invisible, they play an important role in software development, file sharing and cross-platform compatibility. Understanding the difference between LF and CRLF helps prevent formatting inconsistencies, unnecessary version control changes and platform-specific execution problems. By choosing a consistent line-ending format, configuring Git correctly and using tools to detect and convert hidden formatting characters, developers can build projects that remain reliable and easy to collaborate on regardless of the operating system being used.