花样年华 的个人资料『花样年华』In the Mood For Lo...照片日志列表更多 工具 帮助
11月16日

vim: Getting rid of ^M - mixing dos and unix

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; ^Mimport java.io.IOException;
import org.xml.sax.AttributeList; ^M
import org.xml.sax.HandlerBase; ^Mimport org.xml.sax.SAXException;
/**^M
  * XMLHandler: This class parses the elements contained^M
  * within a XML message and builds a Hashtable^M
[snip]
------------------------------------------------------------------
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.
 
3.It also works with
:%s/\r//g
 
4.If you want to put a map of this in your _vimrc file, then you have to double the ^V character so that it looks like this:
map M :%s/^V^V^M$//g^M
note: if you cut and paste this line, it won't work.  You have to type the control characters as explained above.
It has something to do with it being interpreted multiple times, I think.

评论 (2)

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。

若要添加评论,请使用您的 Windows Live ID 登录(如果您使用过 Hotmail、Messenger 或 Xbox LIVE,您就拥有 Windows Live ID)。登录


还没有 Windows Live ID 吗?请注册

匿名 的图片
Celia 发表:
Hope everything goes well.....
11 月 18 日
匿名 的图片
X-Ray 发表:
晕~~好不容易过来一次,什么都看不懂,:-(
11 月 16 日

引用通告

此日志的引用通告 URL 是:
http://tonyxuli.spaces.live.com/blog/cns!C5F02EC8E7FFDA50!525.trak
引用此项的网络日志