vimtutor

vim和emacs是Linux下最流行的代码编辑器,某人说vimtutor是vim最好的入门教材。在网络搜索到两篇文章,分别为中英文版的vimtutor。

 

===============================================================================
= 《 V I M程 》 —— 版本 1.5 =
===============================================================================
vim 是一个具有很多命令的功能非常强大的编辑器。限于篇幅,在本教程当中
就不详细介绍了。本教程的设计目标是讲述一些必要的基本命令,而掌握好这
些命令,您就能够很容易将vim当作一个通用的万能编辑器来使用了。

完成本教程的内容大约需要25-30分钟,取决于您训练的时间。

每一节的命令操作将会更改本文。推荐您复制本文的一个副本,然后在副本上
进行训练(如果您是通过"vimtutor"来启动教程的,那么本文就已经是副本了)。

切记一点∶本教程的设计思路是在使用中进行学习的。也就是说,您需要通过
执行命令来学习它们本身的正确用法。如果您只是阅读而不操作,那么您可能
会很快遗忘这些命令的!

好了,现在请确定您的Shift-Lock(大小写锁定键)还没有按下,然后按键盘上
的字母键 j 足够多的次数来移动光标,直到第一节的内容能够完全充满屏幕。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第一讲第一节∶移动光标


※※ 要移动光标,请依照说明分别按下 h、j、k、l 键。 ※※

         ^
         k          提示∶ h 的键位于左边,每次按下就会向左移动。
< h        l >              l 的键位于右边,每次按下就会向右移动。
         j                      j 键看起来很象一支尖端方向朝下的箭头。
        v

1. 请随意在屏幕内移动光标,直至您觉得舒服为止。

2. 按下下行键(j),直到出现光标重复下行。

---> 现在您应该已经学会如何移动到下一讲吧。

3. 现在请使用下行键,将光标移动到第二讲。

提示∶如果您不敢确定您所按下的字母,请按下<ESC>键回到正常(Normal)模式。
然后再次从键盘输入您想要的命令。

提示∶光标键应当也能正常工作的。但是使用hjkl键,在习惯之后您就能够快速
地在屏幕内四处移动光标了。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第一讲第二节∶VIM的进入和退出


!! 特别提示∶敬请阅读完整本一节的内容,然后才能执行以下所讲解的命令。

1. 请按<ESC>键(这是为了确保您处在正常模式)。

2. 然后输入∶ :q! <回车>

---> 这种方式的退出编辑器绝不会保存您进入编辑器以来所做的改动。
如果您想保存更改再退出,请输入∶
:wq <回车>

3. 如果您看到了命令行提示符,请输入能够带您回到本教程的命令,那就是∶

vimtutor <回车>

通常情况下您也可以用这种方式∶

vim tutor <回车>

---> 这里的 'vim' 表示进入vim编辑器,而 'tutor'则是您准备要编辑的文件。

4. 如果您自信已经牢牢记住了这些步骤的话,请从步骤1执行到步骤3退出,然
后再次进入编辑器。接着将光标移动到第一讲第三节来继续我们的教程讲解。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第一讲第三节∶文本编辑之删除


** 在正常(Normal)模式下,可以按下 x 键来删除光标所在位置的字符。**

1. 请将光标移动到本节中下面标记有 ---> 的那一行。

2. 为了修正输入错误,请将光标移至准备删除的字符的位置处。

3. 然后按下 x 键将错误字符删除掉。

4. 重复步骤2到步骤4,直到句子修正为止。

---> The ccow jumpedd ovverr thhe mooon.

5. 好了,该行已经修正了,下一节内容是第一讲第四节。

特别提示∶在您浏览本教程时,不要强行记忆。记住一点∶在使用中学习。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第一讲第四节∶文本编辑之插入


** 在正常模式下,可以按下 i 键来插入文本。**

1. 请将光标移动到本节中下面标记有 ---> 的第一行。

2. 为了使得第一行内容雷同于第二行,请将光标移至文本第一个字符准备插入
的位置。

3. 然后按下 i 键,接着输入必要的文本字符。

4. 所有文本都修正完毕,请按下 <ESC> 键返回正常模式。
重复步骤2至步骤4以便修正句子。

---> There is text misng this .
---> There is some text missing from this line.

5. 如果您对文本插入操作已经很满意,请接着阅读下面的小结。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第一讲小结


1. 光标在屏幕文本中的移动既可以用箭头键,也可以使用 hjkl 字母键。
h (左移) j (下行) k (上行) l (右移)

2. 欲进入vim编辑器(从命令行提示符),请输入∶vim 文件名 <回车>

3. 欲退出vim编辑器,请输入以下命令放弃所有修改∶

<ESC> :q! <回车>

或者输入以下命令保存所有修改∶

<ESC> :wq <回车>

4. 在正常模式下删除光标所在位置的字符,请按∶ x

5. 在正常模式下要在光标所在位置开始插入文本,请按∶

i 输入必要文本 <ESC>

特别提示∶按下 <ESC> 键会带您回到正常模式或者取消一个不期望或者部分完成
的命令。

好了,第一讲到此结束。下面接下来继续第二讲的内容。


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第二讲第一节∶删除类命令


** 输入 dw 可以从光标处删除至一个单字/单词的末尾。**

1. 请按下 <ESC> 键确保您处于正常模式。

2. 请将光标移动到本节中下面标记有 ---> 的那一行。

3. 请将光标移至准备要删除的单词的开始。

4. 接着输入 dw 删除掉该单词。

特别提示∶您所输入的 dw 会在您输入的同时出现在屏幕的最后一行。如果您输
入有误,请按下 <ESC> 键取消,然后重新再来。

---> There are a some words fun that don't belong paper in this sentence.

5. 重复步骤3至步骤4,直至句子修正完毕。接着继续第二讲第二节内容。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第二讲第二节∶其他删除类命令


** 输入 d$ 从当前光标删除到行末。**

1. 请按下 <ESC> 键确保您处于正常模式。

2. 请将光标移动到本节中下面标记有 ---> 的那一行。

3. 请将光标移动到该行的尾部(也就是在第一个点号‘.’后面)。

4. 然后输入 d$ 从光标处删至当前行尾部。

---> Somebody typed the end of this line twice. end of this line twice.


5. 请继续学习第二讲第三节就知道是怎么回事了。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第二讲第三节∶关于命令和对象


删除命令 d 的格式如下∶

[number] d object 或者 d [number] object

其意如下∶
number - 代表执行命令的次数(可选项,缺省设置为 1 )。
d - 代表删除。
object - 代表命令所要操作的对象(下面有相关介绍)。

一个简短的对象列表∶
w - 从当前光标当前位置直到单字/单词末尾,包括空格。
e - 从当前光标当前位置直到单字/单词末尾,但是 *不* 包括空格。
$ - 从当前光标当前位置直到当前行末。

特别提示∶
对于勇于探索者,请在正常模式下面仅按代表相应对象的键而不使用命令,则
将看到光标的移动正如上面的对象列表所代表的一样。


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第二讲第四节∶对象命令的特殊情况


** 输入 dd 可以删除整一个当前行。 **

鉴于整行删除的高频度,VIM 的设计者决定要简化整行删除,仅需要在同一行上
击打两次 d 就可以删除掉光标所在的整行了。

1. 请将光标移动到本节中下面的短句段落中的第二行。
2. 输入 dd 删除该行。
3. 然后移动到第四行。
4. 接着输入 2dd (还记得前面讲过的 number-command-object 吗?) 删除两行。

1) Roses are red,
2) Mud is fun,
3) Violets are blue,
4) I have a car,
5) Clocks tell time,
6) Sugar is sweet
7) And so are you.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第二讲第五节∶撤消类命令


** 输入 u 来撤消最后执行的命令,输入 U 来修正整行。**

1. 请将光标移动到本节中下面标记有 ---> 的那一行,并将其置于第一个错误
处。
2. 输入 x 删除第一个不想保留的字母。
3. 然后输入 u 撤消最后执行的(一次)命令。
4. 这次要使用 x 修正本行的所有错误。
5. 现在输入一个大写的 U ,恢复到该行的原始状态。
6. 接着多次输入 u 以撤消 U 以及更前的命令。
7. 然后多次输入 CTRL-R (先按下 CTRL 键不放开,接着输入 R 键) ,这样就
可以执行恢复命令,也就是撤消掉撤消命令。

---> Fiix the errors oon thhis line and reeplace them witth undo.

8. 这些都是非常有用的命令。下面是第二讲的小结了。


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第二讲小结


1. 欲从当前光标删除至单字/单词末尾,请输入∶dw

2. 欲从当前光标删除至当前行末尾,请输入∶d$

3. 欲删除整行,请输入∶dd

4. 在正常模式下一个命令的格式是∶

[number] command object 或者 command [number] object
其意是∶
number - 代表的是命令执行的次数
command - 代表要做的事情,比如 d 代表删除
object - 代表要操作的对象,比如 w 代表单字/单词,$ 代表到行末等等。
$ (to the end of line), etc.

5. 欲撤消以前的操作,请输入∶u (小写的u)
欲撤消在一行中所做的改动,请输入∶U (大写的U)
欲撤消以前的撤消命令,恢复以前的操作结果,请输入∶CTRL-R

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第三讲第一节∶置入类命令


** 输入 p 将最后一次删除的内容置入光标之后 **

1. 请将光标移动到本节中下面示范段落的首行。

2. 输入 dd 将该行删除,这样会将该行保存到vim的缓冲区中。

3. 接着将光标移动到准备置入的位置的上方。记住∶是上方哦。

4. 然后在正常模式下(<ESC>键进入),输入 p 将该行粘贴置入。

5. 重复步骤2至步骤4,将所有的行依序放置到正确的位置上。

d) Can you learn too?
b) Violets are blue,
c) Intelligence is learned,
a) Roses are red,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第三讲第二节∶替换类命令


** 输入 r 和一个字符替换光标所在位置的字符。**

1. 请将光标移动到本节中下面标记有 ---> 的第一行。

2. 请移动光标到第一个错误的适当位置。

3. 接着输入 r ,这样就能将错误替换掉了。

4. 重复步骤2和步骤3,直到第一行已经修改完毕。

---> Whan this lime was tuoed in, someone presswd some wrojg keys!
---> When this line was typed in, someone pressed some wrong keys!

5. 然后我们继续学校第三讲第三节。

特别提示∶切记您要在使用中学习,而不是在记忆中学习。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第三讲第三节∶更改类命令


** 要改变一个单字/单词的部分或者全部,请输入 cw **

1. 请将光标移动到本节中下面标记有 ---> 的第一行。

2. 接着把光标放在单词 lubw 的字母 u 的位置那里。

3. 然后输入 cw 就可以修正该单词了(在本例这里是输入 ine 。)

4. 最后按 <ESC> 键,然后光标定位到下一个错误第一个准备更改的字母处。

5. 重复步骤3和步骤4,直到第一个句子完全雷同第二个句子。

---> This lubw has a few wptfd that mrrf changing usf the change command.
---> This line has a few words that need changing using the change command.

提示∶请注意 cw 命令不仅仅是替换了一个单词,也让您进入文本插入状态了。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第三讲第四节∶使用c指令的其他更改类命令


** 更改类指令可以使用同删除类命令所使用的对象参数。**

1. 更改类指令的工作方式跟删除类命令是一致的。操作格式是∶

[number] c object 或者c [number]object

2. 对象参数也是一样的,比如 w 代表单字/单词,$代表行末等等。

3. 请将光标移动到本节中下面标记有 ---> 的第一行。

4. 接着将光标移动到第一个错误处。

5. 然后输入 c$ 使得该行剩下的部分更正得同第二行一样。最后按 <ESC> 键。

---> The end of this line needs some help to make it like the second.
---> The end of this line needs to be corrected using the c$ command.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第三讲小结


1. 要重新置入已经删除的文本内容,请输入小写字母 p。该操作可以将已删除
的文本内容置于光标之后。如果最后一次删除的是一个整行,那么该行将置
于当前光标所在行的下一行。

2. 要替换光标所在位置的字符,请输入小写的 r 和要替换掉原位置字符的新字
符即可。

3. 更改类命令允许您改变指定的对象,从当前光标所在位置直到对象的末尾。
比如输入 cw 可以替换当前光标到单词的末尾的内容;输入 c$ 可以替换当
前光标到行末的内容。

4. 更改类命令的格式是∶

[number] c object 或者 c [number]object

下面我们继续学习下一讲。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第四讲第一节∶定位及文件状态


** 输入 CTRL-g 显示当前编辑文件中当前光标所在行位置以及文件状态信息。
输入 SHIFT-G 则直接跳转到文件中的某一指定行。**

提示∶切记要先通读本节内容,之后才可以执行以下步骤!!!

1. 按下 CTRL 键不放开然后按 g 键。然后就会看到页面最底部出现一个状态信
息行,显示的内容是当前编辑的文件名和文件的总行数。请记住步骤3的行号。

2. 按下 SHIFT-G 键可以使得当前光标直接跳转到文件最后一行。

3. 输入您曾停留的行号,然后按下 SHIFT-G。这样就可以返回到您第一次按下
CTRL-g 时所在的行好了。注意∶输入行号时,行号是不会在屏幕上显示出来
的。

4. 如果愿意,您可以继续执行步骤1至步骤三。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第四讲第二节∶搜索类命令


** 输入 / 以及尾随的字符串可以用以在当前文件中查找该字符串。**

1. 在正常模式下输入 / 字符。您此时会注意到该字符和光标都会出现在屏幕底
部,这跟 : 命令是一样的。

2. 接着输入 errroor <回车>。那个errroor就是您要查找的字符串。

3. 要查找同上一次的字符串,只需要按 n 键。要向相反方向查找同上一次的字
符串,请输入 Shift-N 即可。

4. 如果您想逆向查找字符串,请使用 ? 代替 / 进行。

---> When the search reaches the end of the file it will continue at the start.

"errroor" is not the way to spell error; errroor is an error.

提示∶如果查找已经到达文件末尾,查找会自动从文件头部继续查找。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第四讲第三节∶配对括号的查找


** 按 % 可以查找配对的括号 )、]、}。**

1. 把光标放在本节下面标记有 --> 那一行中的任何一个 (、[ 或 { 处。

2. 接着按 % 字符。

3. 此时光标的位置应当是在配对的括号处。

4. 再次按 % 就可以跳回配对的第一个括号处。

---> This ( is a test line with ('s, ['s ] and {'s } in it. ))

提示∶在程序调试时,这个功能用来查找不配对的括号是很有用的。


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第四讲第四节∶修正错误的方法之一


** 输入 :s/old/new/g 可以替换 old 为 new。**

1. 请将光标移动到本节中下面标记有 ---> 的那一行。

2. 输入 :s/thee/the <回车> 。请注意该命令只改变光标所在行的第一个匹配
串。

3. 输入 :s/thee/the/g 则是替换全行的匹配串。

---> the best time to see thee flowers is in thee spring.

4. 要替换两行之间出现的每个匹配串,请输入 :#,#s/old/new/g (#,#代表的是
两行的行号)。输入 :%s/old/new/g 则是替换整个文件中的每个匹配串。


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第四讲小结


1. Ctrl-g 用于显示当前光标所在位置和文件状态信息。Shift-G 用于将光标跳
转至文件最后一行。先敲入一个行号然后按 Shift-G 则是将光标移动至该行
号代表的行。

2. 输入 / 然后紧随一个字符串是则是在当前所编辑的文档中向后查找该字符串。
输入问号 ? 然后紧随一个字符串是则是在当前所编辑的文档中向前查找该字
符串。完成一次查找之后按 n 键则是重复上一次的命令,可在同一方向上查
找下一个字符串所在;或者按 Shift-N 向相反方向查找下该字符串所在。

3. 如果光标当前位置是括号(、)、[、]、{、},按 % 可以将光标移动到配对的
括号上。

4. 在一行内替换头一个字符串 old 为新的字符串 new,请输入 :s/old/new
在一行内替换所有的字符串 old 为新的字符串 new,请输入 :s/old/new/g
在两行内替换所有的字符串 old 为新的字符串 new,请输入 :#,#s/old/new/g
在文件内替换所有的字符串 old 为新的字符串 new,请输入 :%s/old/new/g
进行全文替换时询问用户确认每个替换需添加 c 选项,请输入 :%s/old/new/gc


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第五讲第一节∶在 VIM 内执行外部命令的方法


** 输入 :! 然后紧随著输入一个外部命令可以执行该外部命令。**

1. 按下我们所熟悉的 : 命令设置光标到屏幕底部。这样就可以让您输入命令了。

2. 接着输入感叹号 ! 这个字符,这样就允许您执行外部的 shell 命令了。

3. 我们以 ls 命令为例。输入 !ls <回车> 。该命令就会列举出您当前目录的
内容,就如同您在命令行提示符下输入 ls 命令的结果一样。如果 !ls 没起
作用,您可以试试 :!dir 看看。

---> 提示∶ 所有的外部命令都可以以这种方式执行。

---> 提示∶ 所有的 : 命令都必须以 <回车> 告终。


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第五讲第二节∶关于保存文件的更多信息


** 要将对文件的改动保存到文件中,请输入 :w FILENAME 。**

1. 输入 :!dir 或者 :!ls 获知当前目录的内容。您应当已知道最后还得敲
<回车> 吧。

2. 选择一个尚未存在文件名,比如 TEST 。

3. 接着输入 :w TEST (此处 TEST 是您所选择的文件名。)

4. 该命令会以 TEST 为文件名保存整个文件 (VIM 教程)。为了确保正确保存,
请再次输入 :!dir 查看您的目录列表内容。

---> 请注意∶如果您退出 VIM 然后在以文件名 TEST 为参数进入,那么该文件内
容应该同您保存时的文件内容是完全一样的。

5. 现在您可以通过输入 :!rm TEST 来删除 TEST 文件了。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第五讲第三节∶一个具有选择性的保存命令


** 要保存文件的部分内容,请输入 :#,# w FILENAME **

1. 再来执行一次 :!dir 或者 :!ls 获知当前目录的内容,然后选择一个合适的
不重名的文件名,比如 TEST 。

2. 接着将光标移动至本页的最顶端,然后按 CTRL-g 找到该行的行号。别忘了
行号哦。

3. 接着把光标移动至本页的最底端,再按一次 CTRL-g 。也别忘了这个行好哦。

4. 为了只保存文章的某个部分,请输入 :#,# w TEST 。这里的 #,# 就是上面
要求您记住的行号(顶端行号,底端行号),而 TEST 就是选定的文件名。

5. 最后,用 :!dir 确认文件是否正确保存。但是这次先别删除掉。


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第五讲第四节∶提取和合并文件


** 要向当前文件中插入另外的文件的内容,请输入 :r FILENAME **

1. 请键入 :!dir 确认您前面创建的 TEST 文件还在。

2. 然后将光标移动至当前页面的顶端。

特别提示∶ 执行步骤3之后您将看到第五讲第三节,请届时再往下移动回到这里来。

3. 接着通过 :r TEST 将前面创建的名为 TEST 的文件提取进来。

特别提示∶您所提取进来的文件将从光标所在位置处开始置入。

4. 为了确认文件已经提取成功,移动光标回到原来的位置就可以注意有两份第
五讲第三节,一份是原本,另外一份是来自文件的副本。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第五讲小结


1. :!command 用于执行一个外部命令 command。

请看一些实际例子∶
:!dir - 用于显示当前目录的内容。
:!rm FILENAME - 用于删除名为 FILENAME 的文件。

2. :w FILENAME 可将当前 VIM 中正在编辑的文件保存到名为 FILENAME 的文
件中。

3. :#,#w FILENAME 可将当前编辑文件第 # 行至第 # 行的内容保存到文件
FILENAME 中。

4. :r FILENAME 可提取磁盘文件 FILENAME 并将其插入到当前文件的光标位置
后面。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第六讲第一节∶打开类命令


** 输入 o 将在光标的下方打开新的一行并进入插入模式。**

1. 请将光标移动到本节中下面标记有 ---> 的那一行。

2. 接着输入小写的 o 在光标 *下方* 打开新的一行并进入插入模式。

3. 然后复制标记有 ---> 的行并按 <ESC> 键退出插入模式而进入正常模式。

---> After typing o the cursor is placed on the open line in Insert mode.

4. 为了在光标 *上方* 打开新的一行,只需要输入大写的 O 而不是小写的 o
就可以了。请在下行测试一下吧。当光标处在在该行上时,按 Shift-O可以
在该行上方新开一行。

Open up a line above this by typing Shift-O while the cursor is on this line.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第六讲第二节∶光标后插入类命令


** 输入 a 将可在光标之后插入文本。 **

1. 请在正常模式下通过输入 $ 将光标移动到本节中下面标记有 ---> 的第一行
的末尾。

2. 接着输入小写的 a 则可在光标之后插入文本了。大写的 A 则可以直接在行
末插入文本。

提示∶输入大写 A 的操作方法可以在行末插入文本,避免了输入 i,光标定位到
最后一个字符,输入的文本,<ESC> 回复正常模式,箭头右键移动光标以及
x 删除当前光标所在位置字符等等诸多繁杂的操作。

3. 操作之后第一行就可以补充完整了。请注意光标后插入文本与插入模式是基
本完全一致的,只是文本插入的位置定位稍有不同罢了。

---> This line will allow you to practice
---> This line will allow you to practice appending text to the end of a line.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第六讲第三节∶另外一个置换类命令的版本


** 输入大写的 R 可连续替换多个字符。**

1. 请将光标移动到本节中下面标记有 ---> 的第一行。

2. 移动光标到第一行中不同于标有 ---> 的第二行的第一个单词的开始,即单
词 last 处。

3. 然后输入大写的 R 开始把第一行中的不同于第二行的剩余字符逐一输入,就
可以全部替换掉原有的字符而使得第一行完全雷同第二行了。

---> To make the first line the same as the last on this page use the keys.
---> To make the first line the same as the second, type R and the new text.

4. 请注意∶如果您按 <ESC> 退出置换模式回复正常模式,尚未替换的文本将仍
然保持原状。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第六讲第四节∶设置类命令的选项


** 设置可使查找或者替换可忽略大小写的选项 **


1. 要查找单词 ignore 可在正常模式下输入 /ignore 。要重复查找该词,可以
重复按 n 键。

2. 然后设置 ic 选项(ic就是英文忽略大小写Ignore Case的首字母缩写词),即
输入∶
:set ic

3. 现在可以通过键入 n 键再次查找单词 ignore。重复查找可以重复键入 n 键。

4. 然后设置 hlsearch 和 incsearch 这两个选项,输入以下内容∶
:set hls is

5. 现在可以再次输入查找命令,看看会有什么效果∶
/ignore

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第六讲小结


1. 输入小写的 o 可以在光标下方打开新的一行并将光标置于新开的行首,进入
插入模式。
输入大写的 O 可以在光标上方打开新的一行并将光标置于新开的行首,进入
插入模式。

2. 输入小写的 a 可以在光标所在位置之后插入文本。
输入大写的 A 可以在光标所在行的行末之后插入文本。

3. 输入大写的 R 将进入替换模式,直至按 <ESC> 键退出替换模式而进入正常
模式。

4. 输入 :set xxx 可以设置 xxx 选项。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第七讲∶在线帮助命令

** 使用在线帮助系统 **

Vim 拥有一个细致全面的在线帮助系统。要启动该帮助系统,请选择如下三种方
法之一∶
- 按下 <HELP> 键 (如果键盘上有的话)
- 按下 <F1> 键 (如果键盘上有的话)
- 输入 :help <回车>

输入 :q <回车> 可以关闭帮助窗口。

提供一个正确的参数给":help"命令,您可以找到关于该主题的帮助。请试验以
下参数(可别忘了按回车键哦。:)∶

:help w <回车>
:help c_<T <回车>
:help insert-index <回车>
:help user-manual <回车>


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
第八讲∶创建一个启动脚本

** 启用vim的功能 **

Vim的功能特性要比vi多得多,但大部分功能都没有缺省激活。为了启动更多的
功能,您得创建一个vimrc文件。

1. 开始编辑vimrc文件,这取决于您所使用的操作系统∶

:edit ~/.vimrc 这是Unix系统所使用的命令
:edit $VIM/_vimrc 这是Windows系统所使用的命令

2. 接着导入vimrc范例文件∶

:read $VIMRUNTIME/vimrc_example.vim

3. 保存文件,命令为∶

:write

在下次您启动vim的时候,编辑器就会有了语法高亮的功能。您可以继续把您喜
欢的其它功能设置添加到这个vimrc文件中。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim 教程到此结束。本教程只是为了简明地介绍一下vim编辑器,但已足以让您
很容易学会使用本编辑器了。毋庸质疑,vim还有很多很多的命令,本教程所介
绍的还差得远著呢。所以您要精通的话,还望继续努力哦。下一步您可以阅读
vim手册,使用的命令是∶
:help user-manual

为了更进一步的参考和学习,以下这本书值得推荐∶

Vim - Vi Improved - 作者∶Steve Oualline
出版社∶New Riders

这是第一本完全讲解vim的书籍。对于初学者特别有用。其中还包含有大量实例
和图示。欲知详情,请访问 http://iccf-holland.org/click5.html

以下这本书比较老了而且内容主要是vi而不是vim,但是也值得推荐∶

Learning the Vi Editor - 作者∶Linda Lamb
出版社∶O'Reilly & Associates Inc.

这是一本不错的书,通过它您几乎能够了解到全部vi能够做到的事情。此书的第
六个版本也包含了一些关于vim的信息。

本教程是由来自Calorado School of Minese的Michael C. Pierce、Robert K.
Ware 所编写的,其中来自Colorado State University的Charles Smith提供了
很多创意。编者通信地址是∶

bware@mines.colorado.edu

本教程已由Bram Moolenaar专为vim进行修订。

 

===========================================================

W e l c o m e t o t h e

V I M T u t o r - Version 1.7

===========================================================

Vim is a very powerful editor that has many commands, too many to explain in a tutor such as this.This tutor is designed to describe enough of the commands that you will be able to easily use Vim asan all-purpose editor.

The approximate time required to complete the tutor is 25-30 minutes, depending upon how much time is spent with experimentation.

ATTENTION:

The commands in the lessons will modify the text. Make a copy of this file to practise on (if you started "vimtutor" this is already a copy).

It is important to remember that this tutor is set up to teach by use. That means that you need to execute the commands to learn them properly.If you only read the text, you will forget the commands!

Now, make sure that your Shift-Lock key is NOT depressed and press the j key enough times to move the cursor so that Lesson 1.1 completely fills the screen.

Lesson 1.1:MOVING THE CURSOR

** To move the cursor, press the h,j,k,l keys as indicated. **

^

k Hint:The h key is at the left and moves left.

< h l > The l key is at the right and moves right.

j The j key looks like a down arrow.

v

1. Move the cursor around the screen until you are comfortable.

2. Hold down the down key (j) until it repeats.

Now you know how to move to the next lesson.

3. Using the down key, move to Lesson 1.2.

NOTE: If you are ever unsure about something you typed, press <ESC> to place you in Normal mode.Then retype the command you wanted.

NOTE: The cursor keys should also work. But using hjkl you will be able to move around much faster, once you get used to it.Really!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Lesson 1.2: EXITING VIM

!! NOTE: Before executing any of the steps below, read this entire lesson!!

1. Press the <ESC> key (to make sure you are in Normal mode).

2. Type: :q! <ENTER>.

This exits the editor, DISCARDING any changes you have made.

3. When you see the shell prompt, type the command that got you into this tutor.That would be:vimtutor <ENTER>

4. If you have these steps memorized and are confident, execute steps

1 through 3 to exit and re-enter the editor.

NOTE: :q! <ENTER> discards any changes you made. In a few lessons you will learn how to save the changes to a file.

5. Move the cursor down to Lesson 1.3.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 1.3: TEXT EDITING - DELETION

** Press x to delete the character under the cursor. **

1. Move the cursor to the line below marked --->.

2. To fix the errors, move the cursor until it is on top of the character

to be deleted.

3. Press the x key to delete the unwanted character.

4. Repeat steps 2 through 4 until the sentence is correct.

---> The ccow jumpedd ovverr thhe mooon.

5. Now that the line is correct, go on to Lesson 1.4.

NOTE: As you go through this tutor, do not try to memorize, learn by usage.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Lesson 1.4: TEXT EDITING - INSERTION

** Press i to insert text. **

1. Move the cursor to the first line below marked --->.

2. To make the first line the same as the second, move the cursor on

Top of the first character AFTER where the text is to be inserted.

3. Press i and type in the necessary additions.

4. As each error is fixed press <ESC> to return to Normal mode.

Repeat steps 2 through 4 to correct the sentence.

---> There is text misng this .

---> There is some text missing from this line.

5. When you are comfortable inserting text move to lesson 1.5.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Lesson 1.5: TEXT EDITING - APPENDING

** PressAto append text. **

1. Move the cursor to the first line below marked --->.

It does not matter on what character the cursor is in that line.

2. Press A and type in the necessary additions.

3. As the text has been appended press <ESC> to return to Normal

mode.

4. Move the cursor to the second line marked ---> and repeat steps 2

and 3 to correct this sentence.

---> There is some text missing from th

There is some text missing from this line.

---> There is also some text miss

There is also some text missing here.

5. When you are comfortable appending text move to lesson 1.6.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 1.6: EDITING A FILE

** Use:wqto save a file and exit. **

!! NOTE: Before executing any of the steps below, read this entire lesson!!

1. Exit this tutor as you did in lesson 1.2: :q!

Or, if you have access to another terminal, do the following there.

2. At the shell prompt type this command: vim tutor <ENTER>

'vim' is the command to start the Vim editor, 'tutor' is the name of the

file you wish to edit. Use a file that may be changed.

3. Insert and delete text as you learned in the previous lessons.

4. Save the file with changes and exit Vim with: :wq <ENTER>

5. If you have quit vimtutor in step 1 restart the vimtutor and move

down to the following summary.

6. After reading the above steps and understanding them: do it.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Lesson 1 SUMMARY

1. The cursor is moved using either the arrow keys or the hjkl keys.

h (left) j (down)k (up)l (right)

2. To start Vim from the shell prompt type: vim FILENAME <ENTER>

3. To exit Vim type: <ESC> :q! <ENTER> to trash all changes.

OR type: <ESC> :wq<ENTER>to save the changes.

4. To delete the character at the cursor type: x

5. To insert or append text type:

i type inserted text <ESC> insert before the cursor

A type appended text <ESC> append after the line

NOTE: Pressing <ESC> will place you in Normal mode or will cancel

an unwanted and partially completed command.

Now continue with Lesson 2.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 2.1: DELETION COMMANDS

** Type dw to delete a word. **

1. Press <ESC> to make sure you are in Normal mode.

2. Move the cursor to the line below marked --->.

3. Move the cursor to the beginning of a word that needs to be

deleted.

4. Type dw to make the word disappear.

NOTE: The letter d will appear on the last line of the screen as you type

it. Vim is waiting for you to type w . If you see another character

than d you typed something wrong; press <ESC> and start over.

---> There are a some words fun that don't belong paper in this sentence.

5. Repeat steps 3 and 4 until the sentence is correct and go to Lesson 2.2.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 2.2: MORE DELETION COMMANDS

** Type d$ to delete to the end of the line. **

1. Press <ESC> to make sure you are in Normal mode.

2. Move the cursor to the line below marked --->.

3. Move the cursor to the end of the correct line (AFTER the first . ).

4. Type d$ to delete to the end of the line.

---> Somebody typed the end of this line twice. end of this line twice.

5. Move on to Lesson 2.3 to understand what is happening.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 2.3: ON OPERATORS AND MOTIONS

Many commands that change text are made from an operator and a motion. The format for a delete command with theddelete operator is as follows: d motion

Where:

d - is the delete operator.

motion - is what the operator will operate on (listed below).

A short list of motions:

w - until the start of the next word, EXCLUDING its first character.

e - to the end of the current word, INCLUDING the last character.

$ - to the end of the line, INCLUDING the last character.

Thus typing de will delete from the cursor to the end of the word.

NOTE: Pressing just the motion while in Normal mode without an operator will move the cursor as specified.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 2.4: USING A COUNT FOR A MOTION

** Typing a number before a motion repeats it that many times. **

1. Move the cursor to the start of the line marked ---> below.

2. Type 2w to move the cursor two words forward.

3. Type 3e to move the cursor to the end of the third word forward.

4. Type 0 (zero) to move to the start of the line.

5. Repeat steps 2 and 3 with different numbers.

---> This is just a line with words you can move around in.

6. Move on to Lesson 2.5.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 2.5: USING A COUNT TO DELETE MORE

** Typing a number with an operator repeats it that many times. **

In the combination of the delete operator and a motion mentioned above you insert a count before the motion to delete more : d numbermotion

1. Move the cursor to the first UPPER CASE word in the line marked --->.

2. Type d2w to delete the two UPPER CASE words

3. Repeat steps 1 and 2 with a different count to delete the consecutive

UPPER CASE words with one command

---> this ABC DE line FGHI JK LMN OP of words is Q RS TUV cleaned up.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 2.6: OPERATING ON LINES

** Type dd to delete a whole line. **

Due to the frequency of whole line deletion, the designers of Vi decided

it would be easier to simply type two d's to delete a line.

1. Move the cursor to the second line in the phrase below.

2. Type dd to delete the line.

3. Now move to the fourth line.

4. Type 2dd to delete two lines.

---> 1) Roses are red,

---> 2) Mud is fun,

---> 3) Violets are blue,

---> 4) I have a car,

---> 5) Clocks tell time,

---> 6) Sugar is sweet

---> 7) And so are you.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 2.7: THE UNDO COMMAND

** Press u to undo the last commands, U to fix a whole line. **

1. Move the cursor to the line below marked ---> and place it on the

first error.

2. Type x to delete the first unwanted character.

3. Now type u to undo the last command executed.

4. This time fix all the errors on the line using the x command.

5. Now type a capital U to return the line to its original state.

6. Now type u a few times to undo the U and preceding commands.

7. Now type CTRL-R (keeping CTRL key pressed while hitting R) a few times to redo the commands (undo the undo's).

---> Fiix the errors oon thhis line and reeplace them witth undo.

8. These are very useful commands. Now move on to the Lesson 2 Summary.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Lesson 2 SUMMARY

1. To delete from the cursor up to the next word type: dw

2. To delete from the cursor to the end of a line type: d$

3. To delete a whole line type: dd

4. To repeat a motion prepend it with a number: 2w

5. The format for a change command is:

operator[number]motion

where:

operator - is what to do, such as d for delete

[number] - is an optional count to repeat the motion

motion - moves over the text to operate on, such asw (word),

$ (to the end of line), etc.

6. To move to the start of the line use a zero: 0

7. To undo previous actions, type: u(lowercase u)

To undo all the changes on a line, type: U (capital U)

To undo the undo's, type: CTRL-R

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 3.1: THE PUT COMMAND

** Type p to put previously deleted text after the cursor. **

1. Move the cursor to the first ---> line below.

2. Type dd to delete the line and store it in a Vim register.

3. Move the cursor to the c) line, ABOVE where the deleted line should go.

4. Type p to put the line below the cursor.

5. Repeat steps 2 through 4 to put all the lines in correct order.

---> d) Can you learn too?

---> b) Violets are blue,

---> c) Intelligence is learned,

---> a) Roses are red,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 3.2: THE REPLACE COMMAND

** Type rx to replace the character at the cursor with x . **

1. Move the cursor to the first line below marked --->.

2. Move the cursor so that it is on top of the first error.

3. Type r and then the character which should be there.

4. Repeat steps 2 and 3 until the first line is equal to the second one.

---> Whan this lime was tuoed in, someone presswd some wrojg keys!

---> When this line was typed in, someone pressed some wrong keys!

5. Now move on to Lesson 3.3.

NOTE: Remember that you should be learning by doing, not memorization.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 3.3: THE CHANGE OPERATOR

** To change until the end of a word, type ce . **

1. Move the cursor to the first line below marked --->.

2. Place the cursor on the u in lubw.

3. Type ce and the correct word (in this case, type ine ).

4. Press <ESC> and move to the next character that needs to be

changed.

5. Repeat steps 3 and 4 until the first sentence is the same as the

second.

---> This lubw has a few wptfd that mrrf changing usf the change operator.

---> This line has a few words that need changing using the change operator.

Notice that ce deletes the word and places you in Insert mode.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 3.4: MORE CHANGES USING c

** The change operator is used with the same motions as delete. **

1. The change operator works in the same way as delete. The format is:c[number]motion

2. The motions are the same, such as w (word) and $ (end of line).

3. Move to the first line below marked --->.

4. Move the cursor to the first error.

5. Type c$ and type the rest of the line like the second and press <ESC>.

---> The end of this line needs some help to make it like the second.

---> The end of this line needs to be corrected using the c$ command.

NOTE: You can use the Backspace key to correct mistakes while typing.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 3 SUMMARY

1. To put back text that has just been deleted, type p . This puts the

deleted text AFTER the cursor (if a line was deleted it will go on the

line below the cursor).

2. To replace the character under the cursor, type r and then the

character you want to have there.

3. The change operator allows you to change from the cursor to where the motion takes you.eg. Typeceto change from the cursor to the end of the word, c$ to change to the end of a line.

4. The format for change is: c [number] motion

Now go on to the next lesson.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 4.1: CURSOR LOCATION AND FILE STATUS

** Type CTRL-G to show your location in the file and the file status.

Type G to move to a line in the file. **

NOTE: Read this entire lesson before executing any of the steps!!

1. Hold down the Ctrl key and press g . We call this CTRL-G. A

message will appear at the bottom of the page with the filename and the position in the file.Remember the line number for Step 3.

NOTE: You may see the cursor position in the lower right corner of the screen This happens when the 'ruler' option is set (see:help 'ruler')

2. Press G to move you to the bottom of the file.

Type gg to move you to the start of the file.

3. Type the number of the line you were on and then G . This will

return you to the line you were on when you first pressed CTRL-G.

4. If you feel confident to do this, execute steps 1 through 3.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 4.2: THE SEARCH COMMAND

** Type / followed by a phrase to search for the phrase. **

1. In Normal mode type the / character. Notice that it and the

Cursor appear at the bottom of the screen as with the : command.

2. Now type 'errroor' <ENTER>. This is the word you want to search for.

3. To search for the same phrase again, simply type n .

To search for the same phrase in the opposite direction, type N .

4. To search for a phrase in the backward direction, use ? instead of/ .

5. To go back to where you came from press CTRL-O (Keep Ctrl down while pressing the letter o).Repeat to go back further.CTRL-I goes forward.

---> "errroor" is not the way to spell error; errroor is an error.

NOTE: When the search reaches the end of the file it will continue at the

start, unless the 'wrapscan' option has been reset.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 4.3: MATCHING PARENTHESES SEARCH

** Type % to find a matching ),], or } . **

1. Place the cursor on any (, [, or { in the line below marked --->.

2. Now type the % character.

3. The cursor will move to the matching parenthesis or bracket.

4. Type % to move the cursor to the other matching bracket.

5. Move the cursor to another (,),[,],{ or } and see what % does.

---> This ( is a test line with ('s, ['s ] and {'s } in it. ))

NOTE: This is very useful in debugging a program with unmatched parentheses!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 4.4: THE SUBSTITUTE COMMAND

** Type :s/old/new/g to substitute 'new' for 'old'. **

1. Move the cursor to the line below marked --->.

2. Type :s/thee/the <ENTER> . Note that this command only

changes the first occurrence of "thee" in the line.

3. Now type :s/thee/the/g . Adding the g flag means to

Substitute globally in the line, change all occurrences of "thee" in the line.

---> thee best time to see thee flowers is in thee spring.

4. To change every occurrence of a character string between two lines,

type :#,#s/old/new/g where #,# are the line numbers of the range of lines where the substitution is to be done.

Type :%s/old/new/g to change every occurrence in the whole file.

Type :%s/old/new/gc to find every occurrence in the whole file,

with a prompt whether to substitute or not.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 4 SUMMARY

1. CTRL-G displays your location in the file and the file status.

G moves to the end of the file.

number G moves to that line number.

gg moves to the first line.

2. Typing / followed by a phrase searches FORWARD for the phrase.

Typing ? followed by a phrase searches BACKWARD for the phrase.

After a search type n to find the next occurrence in the same directionorNto search in the opposite direction.

CTRL-O takes you back to older positions, CTRL-I to newer positions.

3. Typing % while the cursor is on a (,),[,],{, or } goes to its match.

4. To substitute new for the first old in a line type :s/old/new

To substitute new for all 'old's on a line type :s/old/new/g

To substitute phrases between two line #'s type :#,#s/old/new/g

To substitute all occurrences in the file type :%s/old/new/g

To ask for confirmation each time add 'c' :%s/old/new/gc

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 5.1: HOW TO EXECUTE AN EXTERNAL COMMAND

** Type :! followed by an external command to execute that command. **

1. Type the familiar command : to set the cursor at the bottom of the screen.This allows you to enter a command-line command.

2. Now type the ! (exclamation point) character. This allows you toexecute any external shell command.

3. As an example type ls following the ! and then hit <ENTER>.This will show you a listing of your directory, just as if you were at the

shell prompt. Or use :!dir if ls doesn't work.

NOTE: It is possible to execute any external command this way, also with arguments.

NOTE: All : commands must be finished by hitting <ENTER>

From here on we will not always mention it.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Lesson 5.2: MORE ON WRITING FILES

** To save the changes made to the text, type :w FILENAME. **

1. Type :!dir or :!ls to get a listing of your directory.

You already know you must hit <ENTER> after this.

2. Choose a filename that does not exist yet, such as TEST.

3. Now type: :w TEST (where TEST is the filename you chose.)

4. This saves the whole file (the Vim Tutor) under the name TEST.

To verify this, type :!dir or :!ls again to see your directory.

NOTE: If you were to exit Vim and start it again with vim TEST , the file

would be an exact copy of the tutor when you saved it.

5. Now remove the file by typing (MS-DOS): :!del TEST

or (Unix)::!rm TEST

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 5.3: SELECTING TEXT TO WRITE

** To save part of the file, type v motion :w FILENAME **

1. Move the cursor to this line.

2. Press v and move the cursor to the fifth item below. Notice

that the text is highlighted.

3. Press the : character. At the bottom of the screen :'<,'> will

appear.

4. Type w TEST , where TEST is a filename that does not exist yet.Verify that you see:'<,'>w TESTbefore you press Enter.

5. Vim will write the selected lines to the file TEST. Use :!diror!ls to see it.Do not remove it yet! We will use it in the next lesson.

NOTE: Pressing v starts Visual selection. You can move the cursor around to make the selection bigger or smaller. Then you can use an operator to do something with the text. For example, d deletes the text.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 5.4: RETRIEVING AND MERGING FILES

** To insert the contents of a file, type :r FILENAME **

1. Place the cursor just above this line.

NOTE: After executing Step 2 you will see text from Lesson 5.3. Then moveDOWN to see this lesson again.

2. Now retrieve your TEST file using the command :r TEST where

TEST is the name of the file you used.

The file you retrieve is placed below the cursor line.

3. To verify that a file was retrieved, cursor back and notice that there

are now two copies of Lesson 5.3, the original and the file version.

NOTE: You can also read the output of an external command. For example, :r !lsreads the output of the ls command and puts it below the cursor.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 5 SUMMARY

1. :!command executes an external command.

Some useful examples are:

(MS-DOS) (Unix)

:!dir :!ls-shows a directory listing.

:!del FILENAME :!rm FILENAME - removes file FILENAME.

2. :w FILENAME writes the current Vim file to disk with name FILENAME.

3. v motion :w FILENAME saves the Visually selected lines in file

FILENAME.

4. :r FILENAME retrieves disk file FILENAME and puts it below the

cursor position.

5. :r !dir reads the output of the dir command and puts it below the

cursor position.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 6.1: THE OPEN COMMAND

** Type o to open a line below the cursor and place you in Insert mode. **

1. Move the cursor to the line below marked --->.

2. Type the lowercase letter o to open up a line BELOW the cursor

and place you in Insert mode.

3. Now type some text and press <ESC> to exit Insert mode.

---> After typing o the cursor is placed on the open line in Insert mode.

4. To open up a line ABOVE the cursor, simply type a capital O , rather than a lowercaseo.Try this on the line below.

---> Open up a line above this by typing O while the cursor is on this line.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 6.2: THE APPEND COMMAND

** Type a to insert text AFTER the cursor. **

1. Move the cursor to the start of the line below marked --->.

2. Press e until the cursor is on the end of li .

3. Type an a (lowercase) to append text AFTER the cursor.

4. Complete the word like the line below it. Press <ESC> to exit Insert

mode.

5. Use e to move to the next incomplete word and repeat steps 3

and 4.

---> This li will allow you to pract appendi text to a line.

---> This line will allow you to practice appending text to a line.

NOTE: a, i and A all go to the same Insert mode, the only difference is where the characters are inserted.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 6.3: ANOTHER WAY TO REPLACE

** Type a capital R to replace more than one character. **

1. Move the cursor to the first line below marked --->. Move the cursor to the beginning of the firstxxx .

2. Now press R and type the number below it in the second line, so that it replaces the xxx .

3. Press <ESC> to leave Replace mode. Notice that the rest of the line remains unmodified.

4. Repeat the steps to replace the remaining xxx.

---> Adding 123 to xxx gives you xxx.

---> Adding 123 to 456 gives you 579.

NOTE: Replace mode is like Insert mode, but every typed character deletes an existing character.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 6.4: COPY AND PASTE TEXT

** Use the y operator to copy text and p to paste it **

1. Go to the line marked with ---> below and place the cursor after "a)".

2. Start Visual mode with v and move the cursor to just before "first".

3. Type y to yank (copy) the highlighted text.

4. Move the cursor to the end of the next line: j$

5. Type p to put (paste) the text. Then type: a second <ESC> .

6. Use Visual mode to select " item.", yank it with y , move to the end of the next line withj$and put the text there withp .

---> a) this is the first item.

b)

NOTE: you can also use y as an operator; yw yanks one word.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 6.5: SET OPTION

** Set an option so a search or substitute ignores case **

1. Search for 'ignore' by entering: /ignore <ENTER>

Repeat several times by pressing n .

2. Set the 'ic' (Ignore case) option by entering: :set ic

3. Now search for 'ignore' again by pressing n

Notice that Ignore and IGNORE are now also found.

4. Set the 'hlsearch' and 'incsearch' options: :set hls is

5. Now type the search command again and see what happens: /ignore <ENTER>

6. To disable ignoring case enter: :set noic

NOTE: To remove the highlighting of matches enter: :nohlsearch

NOTE: If you want to ignore case for just one search command, use \cin the phrase:/ignore\c<ENTER>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 6 SUMMARY

1. Type o to open a line BELOW the cursor and start Insert mode.

Type O to open a line ABOVE the cursor.

2. Type a to insert text AFTER the cursor.

Type A to insert text after the end of the line.

3. The e command moves to the end of a word.

4. The y operator yanks (copies) text, p puts (pastes) it.

5. Typing a capital R enters Replace mode until <ESC> is pressed.

6. Typing ":set xxx" sets the option "xxx". Some options are:

'ic' 'ignorecase' ignore upper/lower case when searching

'is' 'incsearch' show partial matches for a search phrase

'hls' 'hlsearch' highlight all matching phrases

You can either use the long or the short option name.

7. Prepend "no" to switch an option off: :set noic

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 7.1: GETTING HELP

** Use the on-line help system **

Vim has a comprehensive on-line help system. To get started, try one of

these three:

- press the <HELP> key (if you have one)

- press the <F1> key (if you have one)

- type :help <ENTER>

Read the text in the help window to find out how the help works.

Type CTRL-W CTRL-W to jump from one window to another.

Type :q <ENTER> to close the help window.

You can find help on just about any subject, by giving an argument to the ":help" command.Try these (don't forget pressing <ENTER>):

:help w

:help c_CTRL-D

:help insert-index

:help user-manual

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 7.2: CREATE A STARTUP SCRIPT

** Enable Vim features **

Vim has many more features than Vi, but most of them are disabled by

default. To start using more features you have to create a "vimrc" file.

1. Start editing the "vimrc" file. This depends on your system:

:e ~/.vimrc for Unix

:e $VIM/_vimrc for MS-Windows

2. Now read the example "vimrc" file contents:

:r $VIMRUNTIME/vimrc_example.vim

3. Write the file with: :w

The next time you start Vim it will use syntax highlighting.

You can add all your preferred settings to this "vimrc" file.

For more information type :help vimrc-intro

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 7.3: COMPLETION

** Command line completion with CTRL-D and <TAB> **

1. Make sure Vim is not in compatible mode: :set nocp

2. Look what files exist in the directory: :!ls or :!dir

3. Type the start of a command: :e

4. Press CTRL-D and Vim will show a list of commands that start with "e".

5. Press <TAB> and Vim will complete the command name to ":edit".

6. Now add a space and the start of an existing file name: :edit FIL

7. Press <TAB>. Vim will complete the name (if it is unique).

NOTE: Completion works for many commands. Just try pressing CTRL-D and <TAB>.It is especially useful for:help .

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lesson 7 SUMMARY

1. Type :help or press <F1> or <Help> to open a help window.

2. Type :help cmd to find help on cmd .

3. Type CTRL-W CTRL-W to jump to another window

4. Type :q to close the help window

5. Create a vimrc startup script to keep your preferred settings.

6. When typing a : command, press CTRL-D to see possible completions.Press <TAB> to use one completion.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This concludes the Vim Tutor. It was intended to give a brief overview of the Vim editor, just enough to allow you to use the editor fairly easily. It is far from complete as Vim has many many more commands.Read the usermanual next: ":help user-manual".

For further reading and studying, this book is recommended:

Vim - Vi Improved - by Steve Oualline

Publisher: New Riders

The first book completely dedicated to Vim. Especially useful for beginners.

There are many examples and pictures.

See http://iccf-holland.org/click5.html

This book is older and more about Vi than Vim, but also recommended:

Learning the Vi Editor - by Linda Lamb

Publisher: O'Reilly & Associates Inc.

It is a good book to get to know almost anything you want to do with Vi.

The sixth edition also includes information on Vim.

This tutorial was written by Michael C. Pierce and Robert K. Ware,

Colorado School of Mines using ideas supplied by Charles Smith,

Colorado State University. E-mail: bware@mines.colorado.edu.

Modified for Vim by Bram Moolenaar.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值