开发者

Text file with ^M on each line

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 bun开发者_如何学Cch of ^M at the end of each line.

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


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.


its a windows artefact. You have 2 options

  1. Use an editor where you can specify the save format
  2. Use dos2unix on a unix like system to convert


It is a carriage return. DOS/Windows editors tend to use a carriage return and a line feed, Unix editors like to use just the line feed. Some editors like geany and textpad can detect it and dont show you the ^M, and some will let you do a save as where the choices include unix style or crlf style. There is also the issue of having an EOF at the end of the file that some compilers used to require.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜