Volt
00000000

Guide

How to convert line endings (CRLF and LF) on a Mac

Windows ends a line with two characters, everything else with one, and a file that has been through both is often a mixture. Git warns about it, scripts fail on it, and diffs light up every line. Here is how to see what a file uses and change it.

In Terminal

file notes.txt says "with CRLF line terminators" when it finds them. cat -e notes.txt shows a ^M$ at the end of each Windows line. To convert CRLF to LF, sed -i '' 's/\r$//' notes.txt works with the sed macOS ships; dos2unix and unix2dos are not installed by default but are one brew install dos2unix away.

All of these change every line and tell you nothing about a file that was mixed, or one that ends its lines with something else altogether.

In Volt

The status bar says which ending a file uses — LF, CRLF or CR — and says when a file is mixed. Click it to convert; the conversion is one undoable edit, and the next save writes it.

Pasting makes the clipboard's line endings this file's, so a snippet copied from a Windows document does not smuggle CRLF into a Unix file. Paste and Preserve Formatting (⌃⌘V) leaves them as they were.

The project can decide. .editorconfig's end_of_line and .gitattributes' eol=lf are read when a file opens, and the project has the last word. A language can also say how its new files are written.

Find the stragglers. Project Report… lists every file in a project with its line ending, filtered to Not LF.

The endings other tools do not know

Nine kinds in all. NEL (U+0085) comes from IBM mainframes; LS and PS (U+2028 and U+2029) from Unicode itself; VT and FF from old printers; and LFCR — CRLF backwards — from a handful of systems that got it the other way round. A file separated by any of those has no line feed in it, so most tools open it as one enormous line. Volt reads it as lines and writes it back with the endings it came with, unless you convert.

A form feed is treated as a page mark rather than a line ending, unless the file plainly uses it as one — Emacs has put them in C files for decades.

Questions

How do I see line endings on a Mac?

In Terminal, cat -e shows ^M at the end of Windows lines. In Volt the status bar names the ending and reports a mixed file; Show Invisible Characters marks each one.

How do I convert CRLF to LF on a Mac?

In Terminal: sed -i '' 's/\r$//' file. In Volt: click the line ending in the status bar and pick LF; it is one undoable edit, written on the next save.

What is a mixed line-ending file?

One where some lines end in CRLF and others in LF, usually after being edited on two systems. Volt reports it on opening, and converting makes every line the same.

Will Git care?

Git warns about CRLF when core.autocrlf or .gitattributes says it should. Volt reads .gitattributes' eol setting and follows it.

What is a NEL line ending?

U+0085, the line ending of IBM mainframe text. Most editors do not recognise it; Volt reads and writes it, along with LS, PS, VT, FF and LFCR.

Open the file Git keeps complaining about

Volt is in free beta. Download it now — no account, no sign-up — and try it on the file you have in mind. Each build runs for seven days, and every update gives another seven.

Download Volt for Mac

macOS 13 or later, Apple silicon. About 8 MB, signed and notarised. Everything it does · what changed in this build.

Related