OwnBlog from DimiG    About    Archive    RSS

Emacs strip CR(^M)

Time to time I use the great old text editor Emacs. In spite of advanced age this reliable text editor competes with modern text editors and even some IDEs. If you work with text files on different platforms you may encounter with CR(^M) symbols on every line. This happen If you have a mix of lines that ends with ^M and lines that don’t.

Also if you see ^M in your file, you may have opened a file with DOS-style line endings (carriage return + line feed) while Emacs assumes it has Unix-style line endings (line feed only). (The carriage-return character, sometimes abbreviated as CR, is ^M. The line-feed character, sometimes abbreviated as LF, is ^J)

There are some different ways to cleanup the file from CR(^M). I will show how to do it the simple way.

The first you need open such file by Emacs, then give such command to cleanup the buffer: M-x delete-trailing-whitespace.

Look at the bottom left corner. If you see U: letter that means the text file opened in Unix-style mode. But if you want to save it as Windows/DOS-style document you need to give such command also: C-x RET f dos and save it. After that this text file will be opened correctly in Emacs or even Notepad++.

Any your tips are WELCOME! Have a good day.