Get rid of them ^M
Sometimes I’m given files running rampant with ^M’s all over the place (that’s Ctrl-M, by the way). On my Mac, I was having an oddly strange time getting rid of them in Vi. Then today I happened to run across this tidbit by accident:
Hit “:”. The cursor drops to the bottom of the screen. Type “1,$s/” and then press CTRL-V followed by CTRL-M. When you press CTRL-V nothing appears to happen, but the CTRL-M shows up as “^M”. Continue with “/” and then CTRL-V again. Hit RETURN (which will show up as ^M and you could do that too - I just like it this way) and finally “/g”. On your screen the whole thing looks like:
:1,$s/^M/^M/gWhat does that mean? It means “Starting at line 1 and stopping at the end of the file (1,$), substitute (s) any CTRL-M (/^M/) with Unix CTRL-M (^M/) and do it for the entire line rather than just the first CTRL-M you find (g) (On most other Unixes I’d just do s/^M//g ; I don’t know why Mac OS X didn’t let me do that). It is a little strange that you replace ^M with ^M but get something entirely different, but that’s a subject for another day. The morbidly curious can start by typing “man stty” if they need to know now.
Several people have written to point out that %s is equivalent to 1,$. That’s true, but I never use it because I just prefer being specific.
[Source]