Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text file with ^M on each line

Tags:

text-files

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

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

What is this ^M? How do you prevent it from being inserted?

like image 651
sivabudh Avatar asked Jul 15 '10 21:07

sivabudh


1 Answers

Those are DOS/Windows-style line-endings (to be pedantic that's what they're commonly known as now but most early non-UNIX OSes like CP/M and OS/2 had them as well). On the various Unices, line-endings are \n. In DOS/Windows the line-endings are \r\n (CR+LF or Carriage-Return and Line-Feed). The \r is what shows up as ^M. To remove them in vim, I do:

:%s/^M//

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

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

like image 52
Vivin Paliath Avatar answered Sep 19 '22 16:09

Vivin Paliath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!