Hiding ^M in emacs

Emacs

Emacs Problem Overview


Sometimes I need to read log files that have ^M (control-M) in the line endings. I can do a global replace to get rid of them, but then something more is logged to the log file and, of course, they all come back.

Setting unix-style or dos-style end-of-line encoding doesn't seem to make much difference (but unix-style is my default). I'm using the undecided-(unix|dos) coding system.

I'm on windows, reading log files created by log4net (although log4net obviously isn't the only source of this annoyance).

Any hints?

Emacs Solutions


Solution 1 - 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.

Solution 2 - Emacs

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.

Solution 3 - Emacs

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.

Solution 4 - Emacs

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.

Solution 5 - Emacs

If you'd like to view the log files and simply hide the ^M's rather than actually replace them you can use Drew Adam's highlight extension to do so.

You can either write elisp code or make a keyboard macro to do the following

select the whole buffer
hlt-highlight-regexp-region
C-q C-M
hlt-hide-default-face

This will first highlight the ^M's and then hide them. If you want them back use `hlt-show-default-face'

Solution 6 - Emacs

Edric's answer should get more attention. Johan Bockgård's solution does address the poster's complaint, insofar as it makes the ^M's invisible, but that just masks the underlying problem, and encourages further mixing of Unix and DOS line-endings.

The proper solution would be to do a global M-x replace-regexp to turn all line endings to DOS ones (or Unix, as the case may be). Then close and reopen the file (not sure if M-x revert-buffer would be enough) and the ^M's will either all be invisible, or all be gone.

Solution 7 - Emacs

You can change the display-table entry of the Control-M (^M) character, to make it displayable as whitespace or even disappear totally (vacuous). See the code in library pp-c-l.el (Pretty Control-L) for inspiration. It displays ^L chars in an arbitrary way.

Edited: Oops, I just noticed that @binOr already mentioned this method.

Solution 8 - Emacs

Put this in your .emacs:

(defun dos2unix ()
  "Replace DOS eolns CR LF with Unix eolns CR"
  (interactive)
    (goto-char (point-min))
      (while (search-forward "\r" nil t) (replace-match "")))

Now you can simply call dos2unix and remove all the ^M characters.

Solution 9 - Emacs

If you encounter ^Ms in received mail in Gnus, you can use W c (wash CRs), or

(setq gnus-treat-strip-cr t)

Solution 10 - Emacs

what about using dos2unix, unix2dos (now tofrodos)?

Solution 11 - Emacs

sudeepdino008's answer did not work for me (I could not comment on his answer, so I had to add my own answer.).

I was able to fix it using this code:

(defun dos2unix ()
  "Replace DOS eolns CR LF with Unix eolns CR"
  (interactive)
    (goto-char (point-min))
      (while (search-forward (string ?\C-m) nil t) (replace-match "")))

Solution 12 - Emacs

Like binOr said add this to your %APPDATA%.emacs.d\init.el on windows or where ever is your config.

;; Windows EOL
(defun hide-dos-eol ()
  "Hide ^M in files containing mixed UNIX and DOS line endings."
  (interactive)
  (setq buffer-display-table (make-display-table))
  (aset buffer-display-table ?\^M []))

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

(add-hook 'text-mode-hook 'hide-dos-eol)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRussellView Question on Stackoverflow
Solution 1 - EmacsbinOrView Answer on Stackoverflow
Solution 2 - EmacsEdricView Answer on Stackoverflow
Solution 3 - EmacsashawleyView Answer on Stackoverflow
Solution 4 - EmacsT.E.D.View Answer on Stackoverflow
Solution 5 - EmacsjustinhjView Answer on Stackoverflow
Solution 6 - EmacsSABBATINI LucaView Answer on Stackoverflow
Solution 7 - EmacsDrewView Answer on Stackoverflow
Solution 8 - Emacssudeepdino008View Answer on Stackoverflow
Solution 9 - EmacsBrady TrainorView Answer on Stackoverflow
Solution 10 - EmacsMassagranView Answer on Stackoverflow
Solution 11 - EmacsRMKView Answer on Stackoverflow
Solution 12 - EmacsNicolas Lussier-ClémentView Answer on Stackoverflow