Emacs: Newline Convention CR LF

By Xah Lee. Date: . Last updated: .

To fix current buffer's line ending, see Emacs: Change File Line Ending Between Windows, Linux, Mac

Characters Used for Newline

The following characters are used for newline.

Control Characters for Newline
Emacs Input method
Line FeedLF10\n^JCtrl+q Ctrl+j
Carriage ReturnCR13\r^MCtrl+q Ctrl+m

[see List of ASCII Control Characters]

Newline Convention in Windows, Linux, Mac

Different operating systems use different characters for newline (aka end-of-line character), and or use a combination of them to represent a single newline.

Newline is indicated by 1 or 2 control characters.

Operating System Newline Convention
Operating SystemNewline Convention
Unix, Linux, Mac OS XLF
WindowsCRLF (2 characters)
Mac OS Classic (dated before 2002)CR

Note: Mac OS X prefers LF, but accept the Mac OS Classic's CR too.

Why is emacs showing ^M

^M is a carriage return character "/r", unicode codepoint 13.

If emacs displays many ^M in many lines, that means there's inconsistent line ending in the file.

Find out what char is ^M ^J ^L

How to delete ^M

  1. Goto beginning of the file, then Alt+x query-replace, then insert the Carriage Return char by Ctrl+q Ctrl+m for the find string. For replacement string just press Enter for empty string.
  2. Alt+x set-buffer-file-coding-system, then type one of: {mac, dos, unix}. Then, save the file.

Can I change newline from Windows to unix by just deleting ^M

No. When emacs opens a file, it represent all newline by LF, doesn't matter what's the actual newline convention in the file. If emacs displays “^M” (aka CR), that's because the file has inconsistent line endings.

When you save a file, emacs automatically use the correct newline char when writing the buffer to file, according to the value of buffer-file-coding-system. (Alt+x describe-variable to see variable's value.)

Also, emacs may automatically add a newline to the end of the file when you save it. Which character it adds depends on the value of buffer-file-coding-system.

So, if you manually add/remove newline char to every line, but you didn't change the buffer's buffer-file-coding-system, then when you save, emacs may add a newline char to the end of the file that is inconsistent to what you expect.

The auto adding newline is controlled by the variables require-final-newline and mode-require-final-newline.

Emacs Buffers Always Use LF

In emacs buffer, the newline is always just Line Feed, regardless what operating system. Emacs will display {^J, ^M} only when the file's newline chars cannot be interpreted in a consistent way.

Newline at end of file

Many tools or systems/protocols for reading a file, expect a newline at the end of file.

Many editors automatically add a newline at the end of file when you save the file, if it's not already there. (this is true for emacs, but can be turned off.)

This is important to know, because if you manually change the newline character, but after you save the file, a unexpected newline may be quietly added.

Thanks to Stefan Monnier [http://www.iro.umontreal.ca/~monnier/ ] for a major tip on this newline issue in emacs.

Emacs Line Ending