Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers

up vote 13 down vote accepted

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.

share|improve this answer
    
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
add comment

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.

share|improve this answer
    
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
add comment

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.

Cause:

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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