Windows and, well everybody else, treat en-lines differently. When editing a Windows file under an editor such as vim at the end of each line a ctrl+m character is visibly displayed at the as ^M.
To remove the ^M characters at the end of all lines in vi, do this:
:%s/^V^M//g
The ^v and ^m characters are entered by typing the ctrl+v or ctrl+m respectively. When done correctly you should see this:
:%s/^M//g
In UNIX, you can escape a control character by preceding it with a ctrl+v. The :%s is a basic search and replace command in vi. It tells vi to replace the regular expression between the first and second slashes (^M) with the text between the second and third slashes (nothing in this case). The g at the end directs vi to search and replace globally (all occurrences).
Another vim trick. If you want to do a search/replace on a path, say you have /home/north and want to replace it with /Users/adam the / character has to get escaped a lot. The reason for this is that / is both the path delimiter and the search/replace delimiter. To make things easier vim lets you use any character you want for the search/replace delimiter.
So this:
:%s/\/home\/north/\/Users\/adam/g
Becomes this:
:%s@/home/north@/Users/adam@g
Much nicer!
This is my little corner of the Internet, welcome to it. It is my sounding horn for my views on democracy, the environment, security, computers, and code which is beautiful. I like to ask questions and study the wisdom of the crowd, the democratization of information, and why things are different this time around. I am a dog person, and I have been a Mac user since before it was cool.
Zach
January 11th, 2010 at 11:48 am
You can also specify a carriage return by ‘\r’ instead of ^v+^m so you can do this in fewer keystrokes:
:%s/\r//g
paul
January 30th, 2010 at 7:15 pm
Thanks mate, great tutorial