I am wondering if there is a way to temporarily not display the ^M characters in a file.
I don't want to remove them I just want to not display them.

- 4,445
- 3
- 31
- 44

- 2,541
- 3
- 24
- 28
3 Answers
I use the following function (forgot where I found it):
(defun hide-ctrl-M ()
"Hides 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 []))

- 254
- 2
- 8
-
1It's possible to use that code, but I'd recommend using the features built into Emacs to properly handle [text coding](http://stackoverflow.com/a/14009245/462302). – aculich Dec 23 '12 at 06:58
The GNU Emacs documentation describes how to handle Text Coding using revert-buffer-with-coding-system
:
C-x <RET> r coding <RET>
Revisit the current file using the coding system coding (revert-buffer-with-coding-system).
In your case if the correct system coding isn't detected automatically you can type:
C-x RET r dos RET
to avoid displaying the ^M
characters without actually modifying the file.

- 14,545
- 9
- 64
- 71
-
I've found I need to set the coding to `utf-8-hfs-dos`, rather than simply `dos` (using emacs 25.3.1). – BungleFeet Feb 06 '19 at 13:06
At least with emacs 22.3 this seems to be an issue only if your file has a combination of newline styles, say from editing with a "non-enlightened" editor under both windows unix.
If the lines are consistently terminated w/ ^M modern emacs will note at the bottom that it's [dos] mode and not show the ^M. It's then smart enough to place ^M in the file when you save.
If you've got some odd combination you can try running
$ unix2dos FILE
to get to a good state, after which hopefully you can keep it in DOS mode.
I note that my xemacs 21.4 doesn't have this feature, alas.
There's a discussion here: http://groups.google.com/group/gnu.emacs.help/browse_thread/thread/676113e90825d4e7

- 26,632
- 7
- 60
- 80
-
right but... the suggestion to consider unix2dos was offered as a way to correct the problem where a file contains mixed newlines. In other words, the answer provided here applies to a question the OP *may not have known to ask*. – Cheeso Jun 17 '10 at 13:37