Emacs: Newline Representation ^M ^J ^L

By Xah Lee. Date: . Last updated: .

This page explains End Of Line (aka EOL, newline, line ending) conventions in {Windows, Unix, Mac}, and how to change them with emacs.

Line ending are indicated 1 or 2 non-printable character in ASCII. They are:

Unprintable ASCII Characters Used for Newline
NameAbbrevASCII CodeEscape
Emacs Input method
Horizontal TabHT9\t^ICtrl+q Ctrl+i
Line FeedLF10\n^JCtrl+q Ctrl+j
Carriage ReturnCR13\r^MCtrl+q Ctrl+m

〔➤see List of Unprintable ASCII Characters

Following is the newline convention in different operating systems.

Operating System Newline Convention
Operating SystemEscape NotationCaret NotationNotes
Unix, Linux, Mac OS X\n^JMac OS X prefers \n, but accept the Mac OS Classic's \r too.
Mac OS Classic\r^M

Why does emacs show ^M in a buffer?

If emacs shows that, it's probably because you have mixed characters of ^M and ^J and emacs cannot interpret them consistently as newlines.

To fix it, call set-buffer-file-coding-system, then give one of: {mac, dos, unix}. Then, save the file.

If that does not fix it, you can use find replace to remove it manually.

How to delete ^M manually?

Call query-replaceAlt+%】, then type the Carriage Return char by 【Ctrl+q Ctrl+m】 for the find string. For replacement string just press Enter ↵ for empty string.

What does 【Ctrl+q】 mean?

Ctrl+q】 is the shortcut for the command quoted-insert. It will let you enter the next charater literally. For example, to type a literal tab, press 【Ctrl+q】 then the Tab ↹ key.

The Carriage Return has caret notation of “^M”, so, press 【Ctrl+q Ctrl+m】 will insert it. Tab is “^I”, so 【Ctrl+q Ctrl+i】 inserts a tab. Same for “^J”

For detail about unprintable ASCII chars, their notations, input methods, notation for input methods, see: Emacs's Key Syntax Explained.

Can i change newline convention from Windows to unix by just deleting ^M?

Not really. When emacs opens a file, it represent all newline by “^j” (\n), doesn't matter what's the actual newline convention in the file. If emacs displays “^M” (\r), 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.

Also, emacs may automatically add a newline to the end of the file when you save it. Which character it adds depends on the current file encoding system. So, if you manually change the newline char, emacs may add one that is inconsistent to what you expect.

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

How to know which newline convention is used by emacs for the current file?

Call describe-variableCtrl+h v】, then buffer-file-coding-system.

How to quickly find out what ASCII char are those ^M ^J ^L?

Move your cursor to it, then call describe-char.

How to Change File Line Endings Between Windows/Unix/Mac?

How to change file line endings between Windows/Unix/Mac?

Open the file, then call set-buffer-file-coding-systemCtrl+x Enter ↵ f】. When it prompts you for a coding system, type one of: {mac, dos, unix}. Then, save the file. (on Mac OS X, use “unix”. For Microsoft Windows, use “dos”.)

How to change many file's line ending?

See: Emacs Lisp: Convert Line Ending

Emacs Buffers Always Use LF

In emacs buffer, the newline char is always just Line Feed (\n; “^J”; ASCII 10), regardless what OS you are running emacs on. Emacs will display {^J, ^M} only when the file's newline chars cannot be interpreted in a consistent way.

Thanks to Stefan Monnier for a major tip on this newline issue in emacs.

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from my keyboard store.