-
Notifications
You must be signed in to change notification settings - Fork 28
/
learn-vi-10-TextObjects.html
143 lines (137 loc) · 11.1 KB
/
learn-vi-10-TextObjects.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh" xml:lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="learn-vi.css" />
<title>VIM学习笔记 文本对象(Text Objects)</title>
</head>
<body>
<p>在Vim中,相比针对单个字符进行操作,对于单词、句子和段落等更大范围的文本对象(<strong>text-objects</strong> )执行命令则更有效率。Vim的命令结构示例如下:</p>
<p style="text-indent:2em"><code class="inset">[number]<command>[text object or motion]</code></p>
<p>其中:<strong>number</strong>是指命令作用在几个文本对象之上。比如3个单词;<strong>command</strong>是指执行的具体命令。比如删除或复制;<strong>text object or motion</strong>是指具体的文本对象。比如单词、句子或段落。</p>
<p style="font-weight:bold; border-bottom:1px solid lightgray; border-left:6px solid lightgray; padding:0 0 3px 5px">文本对象的类型</p>
<ul>
<li><code class="inset">iw</code> …inner word</li>
<li><code class="inset">aw</code> …a word</li>
<li><code class="inset">iW</code> …inner <span class="caps">WORD</span></li>
<li><code class="inset">aW</code> …a <span class="caps">WORD</span></li>
<li><code class="inset">is</code> …inner sentence</li>
<li><code class="inset">as</code> …a sentence</li>
<li><code class="inset">ip</code> …inner paragraph</li>
<li><code class="inset">ap</code> …a paragraph</li>
<li><code class="inset">it</code> …inner tag</li>
<li><code class="inset">at</code> …a tag</li>
<li><code class="inset">i(</code> or <code class="inset">i)</code> …inner block …everything between ( and ) excluding the parentheses</li>
<li><code class="inset">a(</code> or <code class="inset">a)</code> …a block …everything between ( and ) including the parenthesis</li>
<li><code class="inset">i<</code> or <code class="inset">i></code> …inner block …everything between < and > excluding the brackets</li>
<li><code class="inset">a<</code> or <code class="inset">a></code> …a block …everything between < and > including the brackets</li>
<li><code class="inset">i{</code> or <code class="inset">i}</code> …inner block …everything between { and } excluding the brackets</li>
<li><code class="inset">a{</code> or <code class="inset">a}</code> …a block …everything between { and } including the brackets</li>
<li><code class="inset">i[</code> or <code class="inset">i]</code> …inner block …everything between [ and ] excluding the square brackets</li>
<li><code class="inset">a[</code> or <code class="inset">a]</code> …a block …everything between [ and ] including the square brackets</li>
<li><code class="inset">i"</code> …inner block</li>
<li><code class="inset">a"</code> …a block</li>
<li><code class="inset">i`</code> …inner block</li>
<li><code class="inset">a`</code> …a block</li>
</ul>
<p style="font-weight:bold; border-bottom:1px solid lightgray; border-left:6px solid lightgray; padding:0 0 3px 5px">文本对象的作用范围</p>
<p><code class="inset">iw</code>表示<strong>inner word</strong>。如果键入<code class="inset">viw</code>命令,那么首先<code class="inset">v</code>将进入选择模式,然后<code class="inset">iw</code>将选中当前单词。</p>
<p><code class="inset">aw</code>表示<strong>a word</strong>,它不但会选中当前单词,还会包含当前单词之后的空格。</p>
<p>以下实例中的红色 <span style="color: red">[ ]</span> 表示作用范围:</p>
<table>
<tr>
<td><code class="inset">iw</code></td>
<td>This is a <span style="color: red">[</span><span style="background-color: yellow">test</span><span style="color: red">]</span> sentence. </td>
</tr>
<tr>
<td><code class="inset">aw</code></td>
<td>This is a <span style="color: red">[</span><span style="background-color: yellow">test </span><span style="color: red">]</span>sentence. </td>
</tr>
<tr>
<td><code class="inset">iW</code></td>
<td>This is a <span style="color: red">[</span><span style="background-color: yellow">…test…</span><span style="color: red">]</span> sentence. </td>
</tr>
<tr>
<td><code class="inset">aW</code></td>
<td>This is a <span style="color: red">[</span><span style="background-color: yellow">…test… </span><span style="color: red">]</span>sentence. </td>
</tr>
<tr>
<td><code class="inset">is</code></td>
<td>…sentence. <span style="color: red">[</span><span style="background-color: yellow">This is a sentence.</span><span style="color: red">]</span> This… </td>
</tr>
<tr>
<td><code class="inset">as</code></td>
<td>…sentence. <span style="color: red">[</span><span style="background-color: yellow">This is a sentence. </span><span style="color: red">]</span>This… </td>
</tr>
<tr>
<td><code class="inset">ip</code></td>
<td>End of previous paragraph.<br/><br/><span style="color: red">[</span><span style="background-color: yellow">This is a paragraph. It has two sentences.</span><span style="color: red">]</span><br/><br/>The next. </td>
</tr>
<tr>
<td><code class="inset">ap</code></td>
<td>End of previous paragraph.<br/><br/><span style="color: red">[</span><span style="background-color: yellow">This is a paragraph. It has two sentences.<br/><br/></span><span style="color: red">]</span>The next. </td>
</tr>
<tr>
<td><code class="inset">i(</code> or <code class="inset">i)</code></td>
<td>1 * (<span style="color: red">[</span><span style="background-color: yellow">2 + 3</span><span style="color: red">]</span>) </td>
</tr>
<tr>
<td><code class="inset">a(</code> or <code class="inset">a)</code></td>
<td>1 * <span style="color: red">[</span><span style="background-color: yellow">(2 + 3)</span><span style="color: red">]</span> </td>
</tr>
<tr>
<td><code class="inset">i<</code> or <code class="inset">i></code></td>
<td>The <<span style="color: red">[</span><span style="background-color: yellow">tag</span><span style="color: red">]</span>> </td>
</tr>
<tr>
<td><code class="inset">a<</code> or <code class="inset">i></code></td>
<td>The <span style="color: red">[</span><span style="background-color: yellow"><tag></span><span style="color: red">]</span> </td>
</tr>
<tr>
<td><code class="inset">i{</code> or <code class="inset">i}</code></td>
<td>some {<span style="color: red">[</span><span style="background-color: yellow"> code block </span><span style="color: red">]</span>} </td>
</tr>
<tr>
<td><code class="inset">a{</code> or <code class="inset">a}</code></td>
<td>some <span style="color: red">[</span><span style="background-color: yellow">{ code block }</span><span style="color: red">]</span> </td>
</tr>
<tr>
<td><code class="inset">i[</code> or <code class="inset">i]</code></td>
<td>some [<span style="color: red">[</span><span style="background-color: yellow"> code block </span><span style="color: red">]</span>] </td>
</tr>
<tr>
<td><code class="inset">a[</code> or <code class="inset">a]</code></td>
<td>some <span style="color: red">[</span><span style="background-color: yellow">[ code block ]</span><span style="color: red">]</span> </td>
</tr>
<tr>
<td><code class="inset">i"</code></td>
<td>The "<span style="color: red">[</span><span style="background-color: yellow">best</span><span style="color: red">]</span>" </td>
</tr>
<tr>
<td><code class="inset">a"</code></td>
<td>The<span style="color: red">[</span><span style="background-color: yellow"> “best”</span><span style="color: red">]</span> </td>
</tr>
<tr>
<td><code class="inset">i`</code></td>
<td>The `<span style="color: red">[</span><span style="background-color: yellow">best</span><span style="color: red">]</span>` </td>
</tr>
<tr>
<td><code class="inset">a`</code></td>
<td>The<span style="color: red">[</span><span style="background-color: yellow"> `best`</span><span style="color: red">]</span> </td>
</tr>
</table>
<p style="font-weight:bold; border-bottom:1px solid lightgray; border-left:6px solid lightgray; padding:0 0 3px 5px">文本对象的应用实例</p>
<p>使用<code class="inset">ci)</code>命令,可以删除括号内的所有内容,但保留括号本身。而<code class="inset">ca)</code>命令,则可以删除括号内的所有内容,以及括号本身。</p>
<p></p><a data-flickr-embed="true" href="https://www.flickr.com/photos/yyq123/31510001262/in/dateposted/" title="ci("><img src="https://c1.staticflickr.com/1/713/31510001262_856698438c_o.png" width="511" height="137" alt="ci("></a><script async src="//embedr.flickr.com/assets/client-code.js" charset="utf-8"></script>
<p>在某个开括号上,点击<code class="inset">%</code>键,光标将可以自动移动到相对应的闭括号上。通过与编辑命令组合,<code class="inset">c%</code>可以实现与<code class="inset">ca)</code>相同的功能。使用<code class="inset">%</code>命令,必须将光标放在括号之上;而使用<code class="inset">a)</code>命令,则光标可以在括号之上或括号内的任何位置。而且<code class="inset">%</code>命令,也是无法实现用<code class="inset">i)</code>命令效果的。</p>
<p><a data-flickr-embed="true" href="https://www.flickr.com/photos/yyq123/31488462042/in/dateposted/" title="ca"><img src="https://c3.staticflickr.com/6/5591/31488462042_27b0a4c75f.jpg" width="500" height="117" alt="ca"></a></p>
<p>使用<code class="inset">cit</code>命令,你甚至不用将光标移动到Tag之内,就可以快速修改其中的内容。</p>
<p><a data-flickr-embed="true" href="https://www.flickr.com/photos/yyq123/31635643735/in/dateposted/" title="cit"><img src="https://c1.staticflickr.com/1/587/31635643735_f3a54a1248_o.png" width="211" height="113" alt="cit"></a></p>
<p>使用<code class="inset">ci"</code>命令,可以快速修改当前行内第一个双引号其中的内容。</p>
<p><a data-flickr-embed="true" href="https://www.flickr.com/photos/yyq123/32806602031/in/dateposted/" title="cidq"><img src="https://c1.staticflickr.com/3/2910/32806602031_f8777582e4_o.png" width="211" height="113" alt="cidq" /></a></p>
<p>使用<code class="inset">ci'</code>命令,可以快速修改当前行内第一个单引号其中的内容。</p>
<p><a data-flickr-embed="true" href="https://www.flickr.com/photos/yyq123/32549956220/in/dateposted/" title="cisq"><img src="https://c2.staticflickr.com/4/3735/32549956220_de33a01d9a_o.png" width="211" height="113" alt="cisq" /></a></p>
<p style="border-top:1px solid lightgray"><span style="float:right">Ver: 1.1</span><span><<a title="在单词间移动" href="http://yyq123.blogspot.com/2009/02/vim_4321.html">上一篇</a> |<a title="笔记列表" href="http://yyq123.github.com/learn-vim/learn-vi-00-List.html"> 目录 </a>| <a title="滚动屏幕" href="http://yyq123.blogspot.com/2009/02/vim_27.html">下一篇</a>></span></p>
</body>
</html>