-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathlearn-vi-17-MultiFile.html
115 lines (107 loc) · 10.5 KB
/
learn-vi-17-MultiFile.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
<!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学习笔记 编辑多个文件(Edit Multiple Files)</title>
</head>
<body>
<p>如果需要同时打开并编辑多个文件,可以使用以下几种方法:</p>
<ul>
<li>在启动vim时,可以指定多个文件做为<a style="font-weight:bold" href="http://yyq123.github.io/learn-vim/learn-vi-16-MultiArguments.html" title="Arguments">参数(Argument)</a>以同时打开多个文件;</li>
<li>在Vim中,新建<a style="font-weight:bold" href="http://yyq123.github.io/learn-vim/learn-vi-14-MultiWindows.html">窗口(Window)</a>用于打开文件;</li>
<li>在Vim中,在任一窗口(Window)内,都可以新建多个<a style="font-weight:bold" href="http://yyq123.github.io/learn-vim/learn-vi-13-MultiBuffers.html" title="Buffer">缓冲区(Buffer)</a>用于编辑不同的文件。</li>
</ul>
<p>以下表格,简单列举了参数、窗口和缓冲区命令的对照关系:</p>
<table summary="Commands" border="2" frame="hsides" rules="all" cellspacing="0" cellpadding="3">
<caption>参数和缓冲区命令对照</caption>
<tr><td><code class="inset">:next</code></td><td>切换到下一文件</td><td><code class="inset">:bnext</code></td></tr>
<tr><td><code class="inset">:previous</code></td><td rowspan="2">切换到上一文件</td><td><code class="inset">:bprevious</code></td></tr>
<tr><td><code class="inset">:Next</code></td><td><code class="inset">:bNext</code></td></tr>
<tr><td><code class="inset">:first</code></td><td>切换到第一个文件</td><td><code class="inset">:bfirst</code></td></tr>
<tr><td><code class="inset">:last</code></td><td>切换到最后一个文件</td><td><code class="inset">:blast</code></td></tr>
<tr><td><code class="inset">:args</code></td><td>显示正在编辑的文件名 </td><td><code class="inset">:buffers</code></td></tr>
<tr><td><code class="inset">:argument [N] </code></td><td>切换至第N号文件</td><td><code class="inset">:buffer [N]</code></td></tr>
</table>
<p></p>
<table summary="Commands" border="2" frame="hsides" rules="all" cellspacing="0" cellpadding="3">
<caption>参数、缓冲区和窗口命令对照</caption>
<tr><td><code class="inset">:snext</code></td><td>分割窗口并切换到下一文件</td><td><code class="inset">:sbnext</code></td></tr>
<tr><td><code class="inset">:sprevious</code></td><td rowspan="2">分割窗口并切换到上一文件</td><td><code class="inset">:sbprevious</code></td></tr>
<tr><td><code class="inset">:sNext</code></td><td><code class="inset">:sbNext</code></td></tr>
<tr><td><code class="inset">:sfirst</code></td><td>分割窗口并切换到第一个文件</td><td><code class="inset">:sbfirst</code></td></tr>
<tr><td><code class="inset">:slast</code></td><td>分割窗口并切换到最后一个文件</td><td><code class="inset">:sblast</code></td></tr>
<tr><td><code class="inset">:sall</code></td><td>分割窗口并显示所有文件</td><td><code class="inset">:sball</code></td></tr>
<tr><td><code class="inset">:sargument [N]</code></td><td>分割窗口并切换至第N号文件</td><td><code class="inset">:sbuffer [N]</code></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>使用<a href="http://yyq123.github.io/learn-vim/learn-vi-53-Mark.html" title="Mark">标记</a>,在不同文件之间拷贝文本:</p>
<ol>
<li>编辑第一文件</li>
<li>执行命令<code class="inset">:split second_file</code>打开另一个窗口并开始编辑第二个文件</li>
<li>使用命令<code class="inset">ctrl+Wp</code>回到含有原始文件的前一个窗口</li>
<li>将光标移动到要拷贝文本的第一行</li>
<li>用命令<code class="inset">ma</code>标记这一行</li>
<li>移动到要拷贝文本的最后一行</li>
<li>执行命令<code class="inset">y'a</code>来复制当前光标位置到所做标记之间的文本</li>
<li>使用命令<code class="inset">ctrl+Wp</code>回到将要放置文本的文件</li>
<li>将光标移到将要插入文本的地方</li>
<li>使用命令<code class="inset">P</code>将复制的文本粘贴到文件中</li>
</ol>
<p>使用可视化模式,在不同文件之间拷贝文本:</p>
<ol>
<li>编辑第一文件</li>
<li>执行命令<code class="inset">:split second_file</code>打开另一个窗口并开始编辑第二个文件</li>
<li>使用命令<code class="inset">ctrl+Wp</code>回到含有原始文件的前一个窗口</li>
<li>将光标移动到要拷贝文本的第一行</li>
<li>执行命令<code class="inset">V</code>进入可视化模式</li>
<li>移动到将要复制文本的最后一行,被选中的文本将会被高亮显示</li>
<li>执行命令<code class="inset">y</code>复制选中的文本</li>
<li>使用命令<code class="inset">ctrl+Wp</code>回到将要放置文本的文件</li>
<li>使用命令<code class="inset">P</code>将复制的文本粘贴到文件中</li>
</ol>
<p>使用<a href="http://yyq123.github.io/learn-vim/learn-vi-12-Register.html" title="Register">寄存器</a>,在不同文件之间拷贝文本:</p>
<ol>
<li>编辑第一文件</li>
<li>执行命令<code class="inset">:split second_file</code>打开另一个窗口并开始编辑第二个文件</li>
<li>使用命令<code class="inset">ctrl+Wp</code>回到含有原始文件的前一个窗口</li>
<li>将光标移动到要拷贝文本的第一行</li>
<li>执行命令<code class="inset">"a3yy</code>将需要复制的行放入寄存器</li>
<li>使用命令<code class="inset">ctrl+Wp</code>回到将要放置文本的文件</li>
<li>使用命令<code class="inset">"ap</code>将复制的文本粘贴到文件中</li>
</ol>
<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">:read filename</code>(可简写为<code class="inset">:r</code>)命令,可读进一个文件并将内容插在当前行的后面。我们也可以在命令中,指明读取内容放置在文件中的特定位置。例如<code class="inset">:0r filename</code>命令,将读取内容放置在文件开头;而<code class="inset">:$r filename</code>命令,则会将读取内容放置在文件末尾。</p>
<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">:write</code>(可简写为<code class="inset">:w</code>)用来写入文件(也就是保存当前文件)。</p>
<p>使用以下命令将保存文本到文件collect.txt中:</p>
<p style="text-indent:2em"><code class="inset">:write collect.txt</code></p>
<p>如果这个文件已经存在,那么就会显示错误信息。如果要强行保存,那么需要使用!选项:</p>
<p style="text-indent:2em"><code class="inset">:write! collect.txt</code></p>
<p>我们也可以向已有文件中追加内容。使用以下命令将正编辑的文件内容追加到collect.txt中:</p>
<p style="text-indent:2em"><code class="inset">:write >> collect.txt</code></p>
<p>使用以下命令,将正编辑的文件的部分内容(第100行到文件末尾)追加到collect.txt中:</p>
<p style="text-indent:2em"><code class="inset">:100,$write >> collect.txt</code></p>
<p>如果这个文件不存在,那么就会显示错误信息。可以使用!选项,强行创建并保存新文件:</p>
<p style="text-indent:2em"><code class="inset">:write! >> collect.txt</code></p>
<p>我们还可以导出文件中的部分内容到其它文件中,以达到分拆文件的目的。以下命令将第10行到文件末尾的内容保存到collect.txt中:</p>
<p style="text-indent:2em"><code class="inset">:10,$write collect.txt</code></p>
<p>以下命令则会将当前行到第100行的内容保存到collect.txt中:</p>
<p style="text-indent:2em"><code class="inset">:.,100write collect.txt</code></p>
<p>在可视化模式下,选择内容,然后使用以下命令也可以写入其它文件:</p>
<p style="text-indent:2em"><code class="inset">:'<,'> write collect.txt</code></p>
<p>使用写入命令不仅可以保存文件,而且还可以将文件重定向到其他程序。在Linux系统中,我们可以使用以下命令将文件发送到打印机:</p>
<p style="text-indent:2em"><code class="inset">:write !lpr</code></p>
<p>注意:命令<code class="inset">:write! lpr</code>与<code class="inset">:write !lpr</code>是不同的,前者是强行保存文件,而后者则是将文件发送到打印机。</p>
<p>命令<code class="inset">:wall</code>可以保存所有已经修改过的文件(包括隐藏缓冲区中的文件)。</p>
<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">:quit</code>可以退出当前文件。而<code class="inset">:qall</code>命令,则可以退出所有打开的文件。</p>
<p>如果文件已修改但没有保存,则会在窗口底部显示警告信息并禁止退出。可以使用以下命令,配置Vinm在此类情况下显示确认对话框:</p>
<p style="text-indent:2em"><code class="inset">:set confirm</code></p>
<p><a href="https://www.flickr.com/photos/yyq123/33413666225/in/dateposted/" title="set_confirm"><img src="https://c1.staticflickr.com/1/736/33413666225_df3ce0c581_o.png" width="436" height="136" alt="set_confirm" /></a></p>
<p>如果想要放弃所做的修改并强行退出,可以使用<code class="inset">:qall!</code>命令。</p>
<p>使用命令<code class="inset">:wqall</code>组合,可以保存所有文件并退出。</p>
<p style="border-top:1px solid lightgray"><span style="float:right">Ver: 2.0 | <a href="mailto:[email protected]">YYQ</a></span><span><<a title="窗口(Window)" href="http://yyq123.github.io/learn-vim/learn-vi-14-MultiWindows.html">上一篇</a> |<a title="笔记列表" href="http://yyq123.github.com/learn-vim/learn-vi-00-List.html"> 目录 </a>| <a title="缓冲区(Buffer)" href="http://yyq123.github.io/learn-vim/learn-vi-13-MultiBuffers.html">下一篇</a>></span></p>
</body>
</html>