VIM TIP26
VimTip 26: Getting rid of ^M - mixing dos and unix
清除混乱的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的情况。如下例所示:
------------------------------------------------------------------
[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.
值得注意的是一些程序没有以本方法识别出换行符,所以在行尾有的是回车符、有的用^M换行(有的两个混合)。这里有这么两种办法来清理文件
1. replace all extraneous ^M:
1.替换所有的无用^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)
确定 ^M是使用 "CTRL-V CTRL-M" 而不是字面上的 ^M。这个正则式将替换所有回车符前的 ^M为空($是为了保证^M出现在行尾)
2. replace all ^M's that need to have carriage returns:
2.替换所有的^M
:%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.
再次注意:确定 ^M是使用 "CTRL-V CTRL-M" 而不是字面上的 ^M。这个正则式将替换所有没有回车符的^M为回车符。
Voila! Clean file. Map this to something if you do it frequently.
瞧!干净多了。如果常常这样操作的话映射成命令。
:help ffs - for more info on file formats
:help ffs -有更多的信息
thanks to jonathan merz, douglas potts, and benji fisher
多谢了: jonathan merz douglas potts benji fisher
Technorati Tags: VI, EDITOR
清除混乱的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]
------------------------------------------------------------------
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.
值得注意的是一些程序没有以本方法识别出换行符,所以在行尾有的是回车符、有的用^M换行(有的两个混合)。这里有这么两种办法来清理文件
1. replace all extraneous ^M:
1.替换所有的无用^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)
确定 ^M是使用 "CTRL-V CTRL-M" 而不是字面上的 ^M。这个正则式将替换所有回车符前的 ^M为空($是为了保证^M出现在行尾)
2. replace all ^M's that need to have carriage returns:
2.替换所有的^M
:%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.
再次注意:确定 ^M是使用 "CTRL-V CTRL-M" 而不是字面上的 ^M。这个正则式将替换所有没有回车符的^M为回车符。
Voila! Clean file. Map this to something if you do it frequently.
瞧!干净多了。如果常常这样操作的话映射成命令。
:help ffs - for more info on file formats
:help ffs -有更多的信息
thanks to jonathan merz, douglas potts, and benji fisher
多谢了: jonathan merz douglas potts benji fisher
Technorati Tags: VI, EDITOR
Comments