<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nonozone &#187; 查找替换</title>
	<atom:link href="http://www.nonozone.net/tag/%e6%9f%a5%e6%89%be%e6%9b%bf%e6%8d%a2/feed" rel="self" type="application/rss+xml" />
	<link>http://www.nonozone.net</link>
	<description>冷眼旁观互联网</description>
	<lastBuildDate>Mon, 16 Jan 2012 10:11:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>VIM中常用的替换模式总结</title>
		<link>http://www.nonozone.net/vim-model-commonly-used-in-the-replacement-summary.html</link>
		<comments>http://www.nonozone.net/vim-model-commonly-used-in-the-replacement-summary.html#comments</comments>
		<pubDate>Mon, 25 May 2009 06:57:08 +0000</pubDate>
		<dc:creator>nonozone</dc:creator>
				<category><![CDATA[奇淫巧技]]></category>
		<category><![CDATA[VIM]]></category>
		<category><![CDATA[查找替换]]></category>

		<guid isPermaLink="false">http://www.nonozone.net/vim-model-commonly-used-in-the-replacement-summary.html</guid>
		<description><![CDATA[最近在FTP的时候，都是用VIM作为我的默认文本编辑软件，这个软件我就不多介绍了，说他是最强大的文本编辑软件也不为过，当然就是因为太强大，所以刚开始使用的时候会觉得稍微有些无从下手，但是习惯以后，就好多了。 因为自己也时常会碰到一些问题无法解决，就把我碰到的一些问题，然后找出的答案放在这里，一是作为自己的一个笔记，而是跟大家分享一下。 今天跟大家分享的是在Vim中如何查找和替换，在记事本里直接用Ctrl+F就可以了，但是在VIM需要如下操作，我们这里用把four替换成4作为例子。 1，简单替换表达式 替换命令可以在全文中用一个单词替换另一个单词： :%s/four/4/g “%” 范围前缀表示在所有行中执行替换。最后的 “g” 标记表示替换行中的所有匹配点。如果仅仅对当前行进行操作，那么只要去掉%即可 &#160; 如果你有一个象 “thirtyfour” 这样的单词，上面的命令会出错。这种情况下，这个单词会被替换成”thirty4″。要解决这个问题，用 “\&#60;” 来指定匹配单词开头： &#160;&#160;&#160;&#160;&#160;&#160; :%s/\&#60;four/4/g 显然，这样在处理 “fourty” 的时候还是会出错。用 “\&#62;” 来解决这个问题： &#160;&#160;&#160;&#160;&#160;&#160; :%s/\&#60;four\&#62;/4/g 如果你在编码，你可能只想替换注释中的 “four”，而保留代码中的。由于这很难指定，可以在替换命令中加一个 “c” 标记，这样，Vim 会在每次替换前提示你： &#160;&#160;&#160;&#160;&#160;&#160; :%s/\&#60;four\&#62;/4/gc 2，删除多余的空格 要删除这些每行后面多余的空格，可以执行如下命令： &#160;&#160;&#160;&#160;&#160;&#160; :%s/\s\+$// 命令前面指明范围是 “%”，所以这会作用于整个文件。”substitute” 命令的匹配模式是 “\s\+$”。这表示行末（$）前的一个或者多个（\+）空格（\s）。替换命令的 “to” 部分是空的：”//”。这样就会删除那些匹配的空白字符。 3，匹配重复性模式 星号项 “*” 规定在它前面的项可以重复任意次。因此: &#160;&#160;&#160;&#160;&#160;&#160; /a* 匹配 “a”，”aa”，”aaa”，等等。但也匹配 “” (空字串)，因为零次也包含在内。星号 “*” 仅仅应用于那个紧邻在它前面的项。因此 “ab*” [...]<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="108" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="解决VIM中文乱码" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.nonozone.net%2Fsolve-vim-chinese-garbled.html&from=http%3A%2F%2Fwww.nonozone.net%2Fvim-model-commonly-used-in-the-replacement-summary.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 102px !important; height: 102px !important;" src="http://www.nonozone.net/wp-content/uploads/2011/09/dll-file.png" width="102px" height="102px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 108px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">解决VIM中文乱码</font>
                    </a>
                </td>
                <td width="108" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="团购和限时购运营模式异同分析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.nonozone.net%2Foperational-mode-similarities-and-differences-of-group-buy-and-limited-buy.html&from=http%3A%2F%2Fwww.nonozone.net%2Fvim-model-commonly-used-in-the-replacement-summary.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 102px !important; height: 102px !important;" src="http://www.nonozone.net/wp-content/uploads/2011/09/dll-file.png" width="102px" height="102px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 108px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">团购和限时购运营模式异同分析</font>
                    </a>
                </td>
                <td width="108" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="国内微博客网站价值讨论" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.nonozone.net%2Fvalue-of-domestic-micro-blog-website-discussion.html&from=http%3A%2F%2Fwww.nonozone.net%2Fvim-model-commonly-used-in-the-replacement-summary.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 102px !important; height: 102px !important;" src="http://www.nonozone.net/wp-content/uploads/2011/09/dll-file.png" width="102px" height="102px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 108px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">国内微博客网站价值讨论</font>
                    </a>
                </td>
                <td width="108" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="团购网和LBS业务模式异同分析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.nonozone.net%2Fcustomers-network-and-the-similarities-and-differences-of-lbs-business-models.html&from=http%3A%2F%2Fwww.nonozone.net%2Fvim-model-commonly-used-in-the-replacement-summary.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 102px !important; height: 102px !important;" src="http://www.nonozone.net/wp-content/uploads/2011/09/dll-file.png" width="102px" height="102px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 108px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">团购网和LBS业务模式异同分析</font>
                    </a>
                </td>
                <td width="108" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Google网站管理员工具界面更新" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.nonozone.net%2Fgoogle-webmaster-tools-interface-update.html&from=http%3A%2F%2Fwww.nonozone.net%2Fvim-model-commonly-used-in-the-replacement-summary.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 102px !important; height: 102px !important;" src="http://static.wumii.com/site_images/2011/09/01/26877454.jpg" width="102px" height="102px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 108px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Google网站管理员工具界面更新</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>最近在FTP的时候，都是用VIM作为我的默认文本编辑软件，这个软件我就不多介绍了，说他是最强大的文本编辑软件也不为过，当然就是因为太强大，所以刚开始使用的时候会觉得稍微有些无从下手，但是习惯以后，就好多了。</p>
<p>因为自己也时常会碰到一些问题无法解决，就把我碰到的一些问题，然后找出的答案放在这里，一是作为自己的一个笔记，而是跟大家分享一下。</p>
<p>今天跟大家分享的是在Vim中如何查找和替换，在记事本里直接用Ctrl+F就可以了，但是在VIM需要如下操作，我们这里用把four替换成4作为例子。</p>
<p>1，简单替换表达式</p>
<p>替换命令可以在全文中用一个单词替换另一个单词：</p>
<p>:%s/four/4/g</p>
<p>“%” 范围前缀表示在所有行中执行替换。最后的 “g” 标记表示替换行中的所有匹配点。如果仅仅对当前行进行操作，那么只要去掉%即可</p>
<p>&#160; 如果你有一个象 “thirtyfour” 这样的单词，上面的命令会出错。这种情况下，这个单词会被替换成”thirty4″。要解决这个问题，用 “\&lt;” 来指定匹配单词开头：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; :%s/\&lt;four/4/g</p>
<p>显然，这样在处理 “fourty” 的时候还是会出错。用 “\&gt;” 来解决这个问题：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; :%s/\&lt;four\&gt;/4/g</p>
<p>如果你在编码，你可能只想替换注释中的 “four”，而保留代码中的。由于这很难指定，可以在替换命令中加一个 “c” 标记，这样，Vim 会在每次替换前提示你：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; :%s/\&lt;four\&gt;/4/gc</p>
<p>2，删除多余的空格</p>
<p>要删除这些每行后面多余的空格，可以执行如下命令：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; :%s/\s\+$//</p>
<p>命令前面指明范围是 “%”，所以这会作用于整个文件。”substitute” 命令的匹配模式是</p>
<p>“\s\+$”。这表示行末（$）前的一个或者多个（\+）空格（\s）。替换命令的 “to” 部分是空的：”//”。这样就会删除那些匹配的空白字符。</p>
<p>3，匹配重复性模式</p>
<p>星号项 “*” 规定在它前面的项可以重复任意次。因此:</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; /a*</p>
<p>匹配 “a”，”aa”，”aaa”，等等。但也匹配 “” (空字串)，因为零次也包含在内。星号 “*” 仅仅应用于那个紧邻在它前面的项。因此 “ab*” 匹配 “a”，”ab”，”abb”,”abbb”，等等。如要多次重复整个字符串，那么该字符串必须被组成一个项。组成一项的方法就是在它前面加 “\(”，后面加 “\)”。因此这个命令:</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; /\(ab\)*</p>
<p>匹配: “ab”，”abab”，”ababab”，等等。而且也匹配 “”。</p>
<p>要避免匹配空字串，使用 “\+”。这表示前面一项可以被匹配一次或多次。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; /ab\+</p>
<p>匹配 “ab”，”abb”，”abbb”，等等。它不匹配 后面没有跟随 “b” 的 “a”。</p>
<p>要匹配一个可选项，用 “\=”。 例如:</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; /folders\=</p>
<p>匹配 “folder” 和 “folders”。</p>
<p>4，指定重复次数</p>
<p>要匹配某一项的特定次数重复，使用 “\{n,m}” 这样的形式。其中 “n” 和 “m” 都是数字。在它前面的那个项将被重复 “n” 到 “m” 次 (|inclusive| 包含 “n” 和 “m”)。例如:</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; /ab\{3,5}</p>
<p>匹配 “abbb”，”abbbb” 以及 “abbbbb”。</p>
<p>&#160; 当 “n” 省略时，被默认为零。当 “m” 省略时，被默认为无限大。当 “,m” 省略时，就表示重复正好 “n” 次。例如:</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; 模式&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 匹配次数</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; \{,4}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 0，1，2，3 或 4</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; \{3,}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 3，4，5，等等</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; \{0,1}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 0 或 1，同 \=</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; \{0,}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 0 或 更多，同 *</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; \{1,}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 1 或 更多，同 \+</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; \{3}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 3</p>
<p>5，多选一匹配</p>
<p>在一个查找模式中，”或” 运算符是 “\|”。例如:</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; /foo\|bar</p>
<p>这个命令匹配了 “foo” 或 “bar”。更多的抉择可以连在后面:</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; /one\|two\|three</p>
<p>匹配 “one”，”two” 或 “three”。</p>
<p>&#160; 如要匹配其多次重复，那么整个抉择结构须置于 “\(” 和 “\)” 之间:</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; /\(foo\|bar\)\+</p>
<p>这个命令匹配 “foo”，”foobar”，”foofoo”，”barfoobar”，等等。</p>
<p>&#160; 再举个例子:</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160; /end\(if\|while\|for\)</p>
<p>这个命令匹配 “endif”，”endwhile” 和 “endfor”。</p>
<p>源文档 &lt;<a href="http://wdicc.com/search-in-vim/">http://wdicc.com/search-in-vim/</a>&gt; </p>
<br /><g:plusone size="" href="http%3A%2F%2Fwww.nonozone.net%2Fvim-model-commonly-used-in-the-replacement-summary.html"></g:plusone><table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="5"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="108" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="解决VIM中文乱码" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.nonozone.net%2Fsolve-vim-chinese-garbled.html&from=http%3A%2F%2Fwww.nonozone.net%2Fvim-model-commonly-used-in-the-replacement-summary.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 102px !important; height: 102px !important;" src="http://www.nonozone.net/wp-content/uploads/2011/09/dll-file.png" width="102px" height="102px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 108px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">解决VIM中文乱码</font>
                    </a>
                </td>
                <td width="108" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="团购和限时购运营模式异同分析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.nonozone.net%2Foperational-mode-similarities-and-differences-of-group-buy-and-limited-buy.html&from=http%3A%2F%2Fwww.nonozone.net%2Fvim-model-commonly-used-in-the-replacement-summary.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 102px !important; height: 102px !important;" src="http://www.nonozone.net/wp-content/uploads/2011/09/dll-file.png" width="102px" height="102px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 108px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">团购和限时购运营模式异同分析</font>
                    </a>
                </td>
                <td width="108" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="国内微博客网站价值讨论" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.nonozone.net%2Fvalue-of-domestic-micro-blog-website-discussion.html&from=http%3A%2F%2Fwww.nonozone.net%2Fvim-model-commonly-used-in-the-replacement-summary.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 102px !important; height: 102px !important;" src="http://www.nonozone.net/wp-content/uploads/2011/09/dll-file.png" width="102px" height="102px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 108px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">国内微博客网站价值讨论</font>
                    </a>
                </td>
                <td width="108" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="团购网和LBS业务模式异同分析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.nonozone.net%2Fcustomers-network-and-the-similarities-and-differences-of-lbs-business-models.html&from=http%3A%2F%2Fwww.nonozone.net%2Fvim-model-commonly-used-in-the-replacement-summary.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 102px !important; height: 102px !important;" src="http://www.nonozone.net/wp-content/uploads/2011/09/dll-file.png" width="102px" height="102px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 108px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">团购网和LBS业务模式异同分析</font>
                    </a>
                </td>
                <td width="108" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="小巧强悍的卸载工具Revo Uninstaller" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fwww.nonozone.net%2Fsmall-powerful-tool-to-uninstall-revo-uninstaller.html&from=http%3A%2F%2Fwww.nonozone.net%2Fvim-model-commonly-used-in-the-replacement-summary.html">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 102px !important; height: 102px !important;" src="http://static.wumii.com/site_images/2011/09/01/26866679.gif" width="102px" height="102px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 108px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">小巧强悍的卸载工具Revo Uninstaller</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="5" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://www.nonozone.net/vim-model-commonly-used-in-the-replacement-summary.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

