Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get rid of annoying ^M lineendings

179 views
Skip to first unread message

olgo

unread,
Apr 22, 2008, 9:50:49 AM4/22/08
to
I keep reading solutions to this problem by means of substituting the
line endings with a global replace but this is not what I need.
I need for the files to be kept as they are, with all kinds of line
endings, but I don't want to see it in the editor.

Thus, my question is:
Is there a way to tell emacs not to show the ^M character altogether?

Thanks for any help,
/Ola

Joost Kremers

unread,
Apr 22, 2008, 10:13:32 AM4/22/08
to

IME this is only a problem when line endings in a file are not consistent.
so my advice would be to make sure each file only has one kind of line
ending, then emacs will simply detect the format correctly and will tell
you in the mode line you're editing a DOS file, and won't show the ^M
characters.


--
Joost Kremers joostk...@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)

Eli Zaretskii

unread,
Apr 23, 2008, 12:59:19 AM4/23/08
to help-gn...@gnu.org
> From: olgo <Ola....@gmail.com>
> Date: Tue, 22 Apr 2008 06:50:49 -0700 (PDT)

This should happen automatically, unless you have a file with
inconsistent lineendings: some with ^M, others without. Typing "C-x
RET c dos RET" immediately before "C-x C-f" that you use to visit the
file will cause Emacs to not display any ^M characters, even if the
lineendings are inconsistent, but if you edit that file and save it,
Emacs will add a ^M character to each line when it saves the file,
which might not be what you want in this case.

IOW, Emacs cannot remember whether there should be a ^M character on a
line by line basis, only for the whole file.


Stefan Reichör

unread,
Apr 23, 2008, 12:39:24 PM4/23/08
to
Joost Kremers <joostk...@yahoo.com> writes:

> olgo wrote:
>> I keep reading solutions to this problem by means of substituting the
>> line endings with a global replace but this is not what I need.
>> I need for the files to be kept as they are, with all kinds of line
>> endings, but I don't want to see it in the editor.
>>
>> Thus, my question is:
>> Is there a way to tell emacs not to show the ^M character altogether?
>
> IME this is only a problem when line endings in a file are not consistent.
> so my advice would be to make sure each file only has one kind of line
> ending, then emacs will simply detect the format correctly and will tell
> you in the mode line you're editing a DOS file, and won't show the ^M
> characters.

I use the following function to remove the trailing ^M from such files:

(defun xsteve-remove-control-M ()
"Remove ^M at end of line in the whole buffer."
(interactive)
(save-match-data
(save-excursion
(let ((remove-count 0))
(goto-char (point-min))
(while (re-search-forward " $" (point-max) t)
(setq remove-count (+ remove-count 1))
(replace-match "" nil nil))
(message (format "%d ^M removed from buffer." remove-count))))))


Stefan.
** Posted from http://www.teranews.com **

Drew Adams

unread,
Apr 23, 2008, 2:19:17 PM4/23/08
to Stefan Reichör, help-gn...@gnu.org
> I use the following function to remove the trailing ^M from
> such files:

M-x replace-string RET C-q RET RET RET

or

C-M-% C-q RET RET RET !

scott....@gmail.com

unread,
Jul 17, 2014, 4:51:29 PM7/17/14
to
Is there a way to execute this every time emacs starts?

Emanuel Berg

unread,
Jul 17, 2014, 6:31:03 PM7/17/14
to
scott....@gmail.com writes:

> Is there a way to execute this every time emacs starts?

Yes, there are hooks: `emacs-startup-hook' - but isn't
this something that should rather go into
`find-file-hook' or perhaps `before-save-hook'?

Because if you use the Emacs startup hook, won't you
get the same situation whenever you `find-file' or the
like from Emacs, i.e., whenever you don't open files by
means of arguments to the emacs shell command?

Anyway, for example, I have

(untab-all delete-trailing-whitespace)

as `before-save-hook'. You could put a call there to
the function that removes those chars.

Hooks are a often an efficient-but-still poor-man's
solution. If you can do something without using them,
that is often better. In this case I can't tell. But if
there isn't an obvious better way to do it, sure, use
hooks.

--
underground experts united

Michael Heerdegen

unread,
Jul 18, 2014, 1:44:20 AM7/18/14
to help-gn...@gnu.org
Joost Kremers <joostk...@yahoo.com> writes:

> IME this is only a problem when line endings in a file are not consistent.
> so my advice would be to make sure each file only has one kind of line
> ending, then emacs will simply detect the format correctly and will tell
> you in the mode line you're editing a DOS file, and won't show the ^M
> characters.

And if Emacs fails to detect the coding correctly:

(info "(emacs) Specify Coding")


Michael.


Michael Heerdegen

unread,
Jul 18, 2014, 1:53:11 AM7/18/14
to help-gn...@gnu.org
scott....@gmail.com writes:

> Is there a way to execute this every time emacs starts?

Why do you want to do this? Emacs handles different file encodings
automatically (the line end convention is part of the coding system).
Normally you don't need to do such things unless your files are garbled
wrt encoding.

If you always open files with the correct encoding - if Emacs guesses
the wrong encoding, you can force the right one:

(info "(emacs) Specify Coding")

- then you don't ever need to perform any replacements in your files.


Michael.


Florian Lindner

unread,
Jul 18, 2014, 2:52:15 AM7/18/14
to help-gn...@gnu.org
For just hiding the ^M I have:

(defun hide-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 []))

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


0 new messages