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 your 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
Notation
Caret
Notation
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.
Windows\r\n^M^J
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'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. (Call 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-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 Alt+x 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 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?

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

Whitespace Topic

  1. Clean Empty Lines
  2. Delete Trailing Whitespace
  3. Delete Whitespace around Cursor
  4. Tabs, Space, Indentation Setup
  5. Make Whitespaces Visible
  6. Newline Representation ^M ^J ^L
  7. Batch Convert Line Ending in Dired
Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

If you have a question, put $5 at patreon and message me.