原 文: http://www.vim.org/tips/tip.php?tip_id=26 翻 译: monkeyXite monkeyXite@gmail.com 修 订: 更新日期: 2006-05-22 VimTip 26: 清除混乱的dos/unix格式换行 If you work in a mixed environment you will often open files that have ^M's in them. An example would be this: 如果你在混合环境下工作的话,你将常常碰到打开的文件中含^M的情况。如下例所示: ------------------------------------------------------------------ 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] ------------------------------------------------------------------ 值得注意的是一些程序没有以本方法识别出换行符,所以在行尾有的是回车符、有的用^M换行(有的两个混合)。这里有这么两种办法来清理文件 1.替换所有的无用^M :%s/^M$//g 确定 ^M是使用 "CTRL-V CTRL-M" 而不是字面上的 ^M。这个正则式将替换所有回车符前的 ^M为空($是为了保证^M出现在行尾) 2.替换所有的^M :%s/^M//g 再次注意:确定 ^M是使用 "CTRL-V CTRL-M" 而不是字面上的 ^M。这个正则式将替换所有没有回车符的^M为回车符。 瞧!干净多了。如果常常这样操作的话映射成命令。 :help ffs 有更多的信息 多谢了: jonathan merz douglas potts benji fisher