I have stack traces that are coming either from a process or remote server that have ^M at the end of the lines. I can disable read only mode and remove trailing white space, but it would be nice if I could just hide the unwanted characters automatically. I tried playing with set-buffer-process-coding-system, but it says no process.

This question is not the same as the many others asking about ^M characters because I'm talking about a read only buffer. There is no file being visited, so I can't set the file encoding.

  • Is the "read only" part relevant? I.e. if you made the buffer writeable, would you want the carriage returns to be made visible again?
    – phils
    Aug 26, 2022 at 0:59
  • No, I don't want to see them; they're distracting. I see your point. The read only part doesn't really matter. The buffers I'm looking at happen to be read only because they are the output of a process. But I would want the ^M gone even if the buffer weren't marked read only.
    – Todd
    Aug 26, 2022 at 12:53

2 Answers 2


You can use Display Tables to achieve what you’re after. Arrange for the following code to be run in those buffers:

(setq buffer-display-table (make-display-table))
(aset buffer-display-table ?\^M [])

(Also posted on comp.emacs many moons ago.)


The accepted answer mostly does what I want, but it needs a little more work. I want to limit this to buffers that aren't files because, as it happens, I have at least one script with a legitimate ^M character. Also sometimes a buffer opens in a window without actually switching to it, so I think I need two hooks here.

(defun tg/hide-carriage-return ()
    (when (and (not buffer-file-name) (not buffer-display-table))
      (setq buffer-display-table (make-display-table))
      (aset buffer-display-table ?\^M [])))
(add-hook 'after-change-major-mode-hook 'tg/hide-carriage-return)
(add-hook 'buffer-list-update-hook 'tg/hide-carriage-return)

I'm not sure if this is exactly correct, but it seems to get the job done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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