The End-of-Line Story

Sunday, April 5, 2009 2:02
Posted in category Linux, Technical, Windows

I got the answer to a mystery that existed with me since very long. When one creates a file in an editor in windows and opens it in unix in vim, the editor shows weird ^M characters at the end of each line.

I read this today. Now I know what that ^M actually stands for. Windows DOS (MS-DOS) and unix have different conventions for representing newlines. Windows uses both Carriage Return (CR) and Line Feed (LF) to represent a new line, whereas for unix, LF alone is a new line and it leaves CR as it is. It is this CR that is being shown as ^M (which is the assigned key stroke to generate a carriage return).

As the document says, because some person thought something some time in the past, we are faced with this confusion of CR LF and LF everywhere. And now we are writing utilities like dos2unix for this purpose !

By the way, the way to insert a CR in a string is ‘\r’ and LF is ‘\n’.

Liked the post? Share it!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Reddit
  • Slashdot
  • Technorati
  • TwitThis
  • Print
  • email

No related posts.

You can leave a response, or trackback from your own site.

2 Responses to “The End-of-Line Story”

  1. Amit says:

    May 16th, 2009 at 00:32

    How to use dos2unix..is it available in windows….How can I checkin a bunch of files in to CVS with that

    ReplyReply
  2. zephyr19 says:

    May 16th, 2009 at 00:35

    @Amit:

    dos2unix is a unix command. I am sure there are other ways in Windows to make a code unix compatible. But I am not sure. I have always done this on the unix side.

    ReplyReply

Leave a Reply