Text file with ^ M on each line

I just received a source code file from a friend. The file was created on UNIX. When I opened it on Windows using NotePad ++, each line had one additional blank line.

Surprised, I downloaded Vim and used it to open the file. Then I saw a bunch of ^ M at the end of each line.

What is it? How do you not insert it?

+6
text-files
source share
3 answers

These are DOS / Windows end-of-line lines (to be pedantic, what they are usually called now, but most of the earlier non-UNIX systems like CP / M and OS / 2 had them too). In various Unices, end-lines \n . On DOS / Windows, line endings are \r\n (CR + LF or Carriage-Return and Line-Feed). \r is what appears as ^M To remove them in vim, I:

 :%s/^M// 

You can get ^ M by doing CTRL + V and then CTRL + M

If you are on a UNIX system, you can use dos2unix .

+11
source share

his window artifact. You have 2 options

  • Use the editor where you can specify the save format
  • Use dos2unix for UNIX system to convert
+2
source share

This is a carriage return. DOS / Windows editors usually use carriage return and line; Unix editors like to use only line feeds. Some editors, such as geany and textpad, may detect it and not show you ^ M, and some of them will let you save where options include unix or crlf. There is also the problem of having EOF at the end of the file, which some compilers required.

0
source share

All Articles