This comes up for me from time to time and I always forget how to deal with it.
Basics, the ^M is a carriage return or newline symbol, but probably not one produced in your OS. You’ll want to switch it to your system’s default newline so it recognizes it.
Here’s how you might do that in vi/vim
It’s doing a replace with the carriage return /r with the system’s default carriage return /r.
Don’t question it, just do it!
But back up the original file just in case I’m wrong