Saturday, 26 January 2008

Convert DOS newline characters to Unix format in Vi

DOS process text files a little differently to Unix systems. If you write a text file in Windows and then open in in Linux, you may see a ^M on the end of each line. This is just the character that is used on DOS based systems when you press the enter key.

 To get rid of each ^M in Vi use the following command:
1,$s/^M//g
To get the ^M character don't type ^ and M! You need to press Ctrl+v and then Ctrl+m

The 1 at the beginning means start at line 1 and the $ means finish on the final line.

s is the search and replace command. It is used in the format of s/string to search for/string that replaces the search string/

The final g is part of the search and replace command. it makes the command global. If you don't have the g on the end only the first instance is replaced.

No comments:

Post a Comment