Vim:Getting rid of ^M With Vim
1. Using Regular Expressions
Ripped gratuitously from http://vim.sourceforge.net/tips/tip.php?tip_id=26
If you work in a mixed environment you will often open files that have ^M's in them. An example would be this:
import java.util.Hashtable; ^M import java.util.Properties; ^M import java.io.IOException; ^M import org.xml.sax.AttributeList; ^M import org.xml.sax.HandlerBase; ^M import org.xml.sax.SAXException;^M /**^M * XMLHandler: This class parses the elements contained^M * within a XML message and builds a Hashtable^M **/^M
Notice that some programs are not consistent in the way they insert the line breaks so you end up with some lines that have both a carrage return and a ^M and some lines that have a ^M and no carrage return (and so blend into one). There are two steps to clean this up.
1. replace all extraneous ^M:
:%s/^M$//g
BE SURE YOU MAKE the ^M USING "CTRL-V CTRL-M" NOT BY TYPING "CARROT M"! This expression will replace all the ^M's that have carriage returns after them with nothing. (The dollar ties the search to the end of a line)
2. replace all ^M's that need to have carriage returns:
:%s/^M/ /g
Once again: BE SURE YOU MAKE the ^M USING "CTRL-V CTRL-M" NOT BY TYPING "CARROT M"! This expression will replace all the ^M's that didn't have carriage returns after them with a carriage return.
Voila! Clean file. Map this to something if you do it frequently.
:help ffs - for more info on file formats
thanks to jonathan merz, douglas potts, and benji fisher
2. Using the Set Method
Ripped gratuitously from http://vim.sourceforge.net/tips/tip.php?tip_id=145
Those of us doomed to work in both the Unix and Windows world have many times encountered
files that were create/editted on systems other that the one we are on at the time of our edits. We
can easily correct the dreaded '^M' at the end of our Unix lines, or make files have more than one
line in DOS by:
To change from <CR><LF> (DOS) to just <LF> (Unix):
:set fileformat=unix :w
Or to change back the other way:
:set fileformat=dos :w
It also works for Apple land:
:set fileformat=mac :w
And to tell the difference:
set statusline=%<%f%h%m%r%=%{&ff}\ %l,%c%V\ %P
This shows what the current file's format is.