How to remove ^M in emacs

carriage-return, emacs, eol, line-breaks, line-endings

Solution

[Searched for a duplicate, but didn't find one about replacing (instead of preventing etc.). If there is one then this one or that one should be closed.]

In Emacs, visit the file that has the `^M` chars. Go to the beginning of the file (`M-<`), then:

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

That is, at the prompt for what to replace, use `Control` + `q` then `Control` + `m`, then `Enter`. At the prompt for what to replace it with, just hit `Enter` (replace it with nothing).

Problem

How can I remove `^M` characters in Emacs? It doesn't work using `dos2unix filename` or `unix2dos filename`. Normally I cannot see any `^M` characters, but this is what came out when using the command `cat -A filename` : Please explain it in plain words... and in detail... ``` cat -A ABC.sh #!/bin/csh -f^M$ ^M$ ^M$ set input = `ls -1 *.py`^M$ echo $input^M$ ```

Original source

Related problems