Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding ^M in emacs

Tags:

emacs

(defun remove-dos-eol ()
  "Do not show ^M in files containing mixed UNIX and DOS line endings."
  (interactive)
  (setq buffer-display-table (make-display-table))
  (aset buffer-display-table ?\^M []))

Solution by Johan Bockgård. I found it here.


Modern versions of emacs know how to handle both UNIX and DOS line endings, so when ^M shows up in the file, it means that there's a mixture of both in the file. When there is such a mixture, emacs defaults to UNIX mode, so the ^Ms are visible. The real fix is to fix the program creating the file so that it uses consistent line-endings.


What about?

C-x RET c dos RET C-x C-f FILENAME RET

I made a file that has two lines, with the second having a carriage return. Emacs would open the file in Unix coding, and switching coding system does nothing. However, the universal-coding-system-argument above works.


I believe you can change the line coding system the file is using to the Unix format with

C-x RET f UNIX RET

If you do that, the mode line should change to add the word "(Unix)", and all those ^M's should go away.