Identify newline characters in text
How to identify newline characters such as LF, CR, CRLF and Unicode line separators, what inspection tools change, and when to keep plain text.
A line break looks like empty space, but the file stores one or more specific characters. Which characters they are affects how other programs split lines, compare files, parse data and display text. This review covers what those characters are, how to make them visible, and whether a visible-newline transformation suits your purpose. No tool-specific test results were supplied for this article. Claims about particular tools are therefore limited to general, documented behavior, and you should check your own environment before relying on it.
Is a newline-revealing transformation suitable for your use?
Making newline characters visible is worth doing when you need to diagnose a problem. Typical cases are a script that fails on a file from another operating system, a CSV that splits rows in the wrong places, a diff that flags every line as changed, or pasted text that breaks lines unexpectedly. The benefit is certainty. You can see whether a line ends in LF, CR, CRLF or a less common Unicode separator.
The key tradeoff is between display and storage. Viewing the characters through an editor setting or an inspection command leaves the text unchanged. Replacing the characters with visible symbols edits the text, and the result is no longer an accurate copy of the original. Use plain text, with the original line endings intact, whenever the text will be run, parsed, compared, submitted or read by other people. Visible symbols are for your own diagnosis only.
Which characters count as newlines
In ASCII, the newline character is Line Feed (LF), decimal 10, hexadecimal 0A, often written as \n. Carriage Return (CR) is decimal 13, hexadecimal 0D, written as \r. Unix-like systems, including Linux and modern macOS, conventionally end lines with LF alone. Windows conventionally uses the pair CR followed by LF (CRLF). Classic Mac OS used CR alone, and files with CR-only endings still turn up occasionally. Unicode adds further characters that some software treats as line or paragraph breaks.
| Character | Code point | ASCII decimal | UTF-8 bytes | Common escape |
| Line Feed (LF) | U+000A | 10 | 0A | \n |
| Carriage Return (CR) | U+000D | 13 | 0D | \r |
| CR + LF (CRLF) | U+000D U+000A | 13, 10 | 0D 0A | \r\n |
| Vertical Tab (VT) | U+000B | 11 | 0B | \v |
| Form Feed (FF) | U+000C | 12 | 0C | \f |
| Next Line (NEL) | U+0085 | not ASCII | C2 85 | \x85 or \u0085 |
| Line Separator (LS) | U+2028 | not ASCII | E2 80 A8 | \u2028 |
| Paragraph Separator (PS) | U+2029 | not ASCII | E2 80 A9 | \u2029 |
Software disagrees about which of these characters ends a line. Most tools treat LF as a line break. Many also handle CRLF, and fewer handle CR alone. Treatment of VT, FF, NEL, LS and PS varies widely. One program may break the line at one of these characters while another shows nothing, shows a placeholder box, or treats it as ordinary text. This inconsistency is the usual reason newline problems are hard to diagnose by eye.
What inspection actually changes
There are three ways to reveal newlines. They differ in whether the stored text is modified.
- Display-only visualization: an editor option, usually called something like show invisibles, show whitespace or render control characters, draws a marker at each line ending. The file is unchanged, and saving it should not alter the endings, though some editors convert line endings on save as a separate setting.
- Inspection output: commands that print escaped characters or byte values write a description of the text to the screen. The source file is not modified.
- Character substitution: a converter replaces each line ending with a visible character, such as the Unicode control pictures ␊ (U+240A, symbol for line feed) and ␍ (U+240D, symbol for carriage return), or an arrow such as ↵. The output is a new string. The original control characters may be removed or kept next to the symbols, depending on the tool.
Substitution is the only one of the three that changes stored characters. If a substituted version is copied back into a program or data file, the program sees symbols rather than line breaks. Before trusting a converter, check its output. Run a sample that contains a known CRLF pair and confirm the result shows two markers, or one combined marker, rather than silently merging or dropping the CR.
How to find newline characters
Searches for show invisible characters, show line endings, line ending detection, CRLF vs LF or hex dump will find the relevant settings and tools. Many code editors display the current line-ending mode, often in a status bar, and offer a command to convert it. Where the setting lives and what it is called differ between editors and versions, so check your editor's own documentation.
On Linux and other Unix-like systems, standard command-line utilities can show line endings without modifying the file. The od command with the -c option prints each character, showing \r and \n as escapes. The file command commonly reports text files as having CRLF or CR line terminators when they are present. GNU cat with -A marks each line end with $ and shows a carriage return as ^M. BSD and macOS versions of cat use different options, so check the manual page on your system.
$ printf 'a\r\nb\n' | od -c
0000000 a \r \n b \n
0000005
The example above shows the general output format. The first line is CR followed by LF, a Windows-style ending. The second line ends with LF alone, a Unix-style ending. A file that mixes the two styles, as this sample does, is a common source of inconsistent behavior. For non-ASCII separators such as NEL, LS or PS, a byte-level view in hexadecimal (for example od -An -tx1) shows the UTF-8 byte sequences listed in the table above.
In programming languages, printing a string's escaped representation is often the fastest way to check. In Python 3, repr() shows LF as \n and CR as \r, and it escapes U+2028 and U+2029 as \u2028 and \u2029 because they are not printable characters. Other languages provide similar functions, but each language has its own escaping rules.
Will the result stay usable after copying or sharing?
Line endings are often changed in transit without any notice. Clipboards, text fields, email clients, version control settings and file transfer tools can convert between LF and CRLF, or remove characters they do not recognize. The HTML standard, for example, specifies that line breaks in a submitted textarea are normalized to CRLF. Version control systems commonly offer settings that convert endings on checkout or commit. Do not assume pasted text is byte-for-byte identical to its source. Inspect it at the destination.
Unicode separators raise their own compatibility problems. Before the ECMAScript 2019 edition, U+2028 and U+2029 were not allowed inside JavaScript string literals, even though JSON permitted them. Older JavaScript environments can therefore fail on JSON-like text that contains these characters. In JavaScript regular expressions, the dot does not match LF, CR, LS or PS unless the dotAll flag is set. When text containing these characters moves between systems, test each destination directly.
Text with substituted symbols causes problems for readers and search. Screen readers may announce control pictures or arrows by name, skip them, or read them inconsistently. No assistive technology testing was supplied for this article, so this behavior is unverified and should be tested with the specific screen reader concerned. Search and find functions will match the symbol rather than a line break. When shared text needs to be readable, accessible or searchable, send the plain text with real line breaks and describe the line endings separately (for example: this file uses CRLF), rather than sharing a version with markers inserted.
Recommendation
Use display-only visualization or an inspection command to identify newline characters. Neither changes the text, and both show exactly which characters are present. Reserve symbol substitution for screenshots, documentation or personal notes, and keep the original file as the working copy. If the goal is to fix inconsistent endings rather than just view them, convert the file explicitly to a single convention, then check the result again with the same inspection method.