Emacs: Newline Representation ^M ^J ^L

By Xah Lee. Date: . Last updated: .

What does ^M mean?

^M is a carriage return character "/r", ASCII code 13.

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

How to delete ^M ?

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

New line is indicated by 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

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 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. (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 current file encoding 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.

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

Alt+x describe-variable , then buffer-file-coding-system.

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

Move cursor to it, then Alt+x describe-char.

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

Open the file, then Alt+x set-buffer-file-coding-system. 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?

Emacs: Convert File 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 [http://www.iro.umontreal.ca/~monnier/ ] for a major tip on this newline issue in emacs.

Put $5 at https://www.patreon.com/xahlee , or goto paypal.com and pay to Xah@XahLee.org . Message me on XahLee discord, say so, ask me questions.
Or Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys


ELisp Examples

ELisp Write Major Mode

Emacs Tutorial

Quick Start

Manage Windows



Find Replace




Rectangle Edit

Line Wrap


View Special File

Editing Brackets

Org Mode


Emacs Efficiency