Remove DOS EOL ^M Character from File

endoflineIf you have the ^M character in your file, it is a Dos/Windows end of line character that is tough to get rid of. You could always ignore it, but the possibility of it corrupting a file is very high. Some editors such as Nano don’t even seem to show this character by default – maybe Nano strips it out – I’m not sure at this point.

If you are a vi warrior like I am, then you don’t really care for using Nano anyway. To remove the DOS EOL character using the vi editor, the easiest way is by following these steps:

vi filename

Press “:” to get to command mode.

Search and Replace all Globally is :%s/^M//g (Press control V then control M) which will replace ^M with nothing.

Then to write and quit enter “:wq”

In summary, here’s the command would look in vi:

vi filename
:%s/^M//g

If you need the character to actually turn into line breaks, try this:

vi filename
:%s/^M/\r/g