tags:

views:

2207

answers:

5
+6  Q: 

Hiding ^M in emacs

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?

A: 

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.

T.E.D.
Not helpful, I think. set-buffer-file-coding-system seems to change the actual contents of the edited file.
hillu
+3  A: 

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'

justinhj
+1  A: 

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.

ashawley
+3  A: 

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.

Edric
+9  A: 
(defun remove-dos-eol ()
  "Removes the disturbing '^M' showing up 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.

binOr