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. Call set-buffer-file-coding-system, then give one of: {mac, dos, unix}. Then, save the file.
  2. Call 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'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?

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

Whitespace Topic

  1. Emacs: Clean Empty Lines
  2. Emacs: Delete Trailing Whitespace
  3. Emacs: Delete Whitespace around Cursor
  4. Emacs: Tabs, Space, Indentation Setup
  5. Emacs: Make Whitespaces Visible
  6. Emacs: Newline Representation ^M ^J ^L
  7. Emacs: Batch Convert Line Ending in Dired
Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs