Changing a file's line endings in Emacs
There are several different conventions for representing the end of a line in a text file:
- Unix systems use a single line feed character (LF,
0x0A
); - Microsoft Windows (and before that, MS-DOS) uses a carrage return followed
by a line feed (CR-LF,
0x0D 0x0A
); and - The original Macintosh used a single carriage return character (CR,
Ox0D
).
Thankfully, a LF-CR pair has never been used!
Converting line endings using emacs
It is possible to change the end-of-line convention using emacs, with
the set-buffer-file-coding-system
function (which is normally mapped
to C-x RET f
). When it prompts you for the new coding system, enter
either “unix”, “dos” or “mac”.
Converting line endings using Unix commands
This is easier than trying to remember cryptic commands like:
tr -d '\r'
sed 's/$/^M/'
And having to worry about getting them to work because
of different variations in sed
and shell environments
(e.g. when using bash the ^M
is typed using Ctrl-v Ctrl-m).
If your system has the unix2dos
and dos2unix
commands installed (e.g. Cygwin
and most Linux distributions do) use them.
Otherwise, emacs lives up to its reputation as the kitchen sink tool.