Why does Emacs insert ^m at the end of every line in my Linux environment?


Emacs Line Return and Windows, Unix, Mac, All That ^M ^J ^L

Q: Why does emacs show ^M in a buffer?

A: The “^M” is ASCII caret notation for unprintable Carriage return char (ASCII 13). 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 and replace to remove it manually.

  • As an addendum C-x RET f is the key chord for set-buffer-file-coding-system, so you can type that instead. I never remember it, though. – quodlibetor Apr 24 '11 at 3:26

I know that this question has been resolved, but I wanted to mention that Dos2Unix is a handy part of a toolkit for file operation between UNIX/DOS/Mac machines. It is an easy command-based approach for adding/removing OS-preferred line endings.

  • Emacs can handle all the newline varieties a file might contain, without needing to modify the file as Dos2Unix does. Possibly worth knowing for those poor benighted souls who don't use God's own editor, though. :) – Aaron Miller Nov 11 '13 at 17:28

I know it´s an old question, but for the record, there are other reasons why ^M appears at the end of files. I just had the same problem. Here is what happened:

  1. Every day I use the same text file on Emacs and never saw ^M. I use this file to take personal notes.
  2. Last time I used the file I copied and pasted the contents of an e-mail I got on MS Outlook.
  3. Today I opened the same old file and every single line had a ^M at the end, and all non-ASCII characters were replaced by strange codes like ???.

How to fix it:

Open the same file on VI (yes, I know, the archenemy) and search for characters that appear in a highlight color. On my terminal it is blue. Remove those characters and everything will work fine again on Emacs.


The problem was caused by some strange quotation mark characters that came from Outlook. The funny thing is that Emacs does not show this problem until you reopen the file, making it hard for you to identify that your file has a problem right away.

  • 1
    I love how solving an Emavs problem involves using Vim. TAKE THAT, EMACS! – Aviator45003 Jul 30 '14 at 19:04

If you are using Emacs in a primarily DOS/Windows environment, Emacs should correctly recognize the line-ending system and deal with it transparently.

You might see ^M suddenly appear in a file because it has been edited, perhaps with a script, that added lines with UNIX line-endings, and now lline endings are mixed.

You can fix these lines by adding ^M back onto them, save the file, kill the buffer, and re-open it. Now it should look normal, and say (DOS) as the encoding marking on the status line.

The regexp to look for is \([^^M]\)$ and replace with \1^M. Remember to use Ctrl-Q to 'quote' the Ctrl-M characters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.