| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
JanisV84
8年前发布

vi(vim)入门简明实例教程---总览全局 快速入门

来自: http://blog.csdn.net/wangdingqiaoit/article/details/7757386


                    vi(vim)入门简明实例教程---总览全局 快速入门
前言 
本文旨在帮助学习 vim 的新手快速掌握 vim 的基本操作。本文 整理自《 The Linux Command Line 》中《 A Gentle Introduction To vi 》一章 。该书籍通俗易懂,容易上手 , 十分实用。下面保留了一部分的英文,旨在帮助更好的理解命令,从而更快的掌握命令。

注明:文章中实验在ubuntu 11下进行。Vi编辑器最初由Bill Joy1976年编写,大多数的Linux系统并不包含真正的vi,而是由vim(vi improved 的缩写)替换了。在多数系统中vim将被链接为vi,假定我们使用的vi实质上是vim程序。

正文

1.Starting And Stopping vi   启动和退出vi

我们需要学习的第一件事就是如何启动和退出vi

启动vim只需要键入

qiaoqiao@ubuntu:~$ vi                                                                                                                

即可,启动后界面如下图所示:



要退出vi,键入

:q         

即可。

如果由于某些原因(通常是编辑了文件而没有保存的情况下)无法退出时,我们可以使用

:q!        

来强制退出。

2.Editing Modes 编辑模式

再次启动vi,这次在命令后加上一个目前不存在的文件名,如下:

qiaoqiao@ubuntu:~$ vi foo.txt                                        

启动后界面如下:




其中波浪号"~"表示该行还没有文字。这里显示了一个空文件,没有任何东西。

第二件我们需要学习的重要事情就是要知道vi是一个模式编辑器。当vi启动的时候,它就出在命令模式下。在这种模式下,任何的键入都将被解释成命令,此时我们开始键入的时候vi将会显得有点混乱。

进入插入模式

要想添加文本到文件,首先我们需要进入插入模式。这可以通过在命令模式下,键入"i"键即可。我们将会看到vi下方的提示如下:

-- INSERT --                                               0,1           All

现在插入下面一段文字:

The qucik brown fox jumped over the lazy dog.                         

要退出插入模式并返回到命令模式,我们可以按Esc键。

保存我们的工作

要保存我们对文件的更改,我们需要在命令模式下键入一个ex命令(ex command)。我们可以先键入一个冒号,然后紧接着一个"w",如下

:w                                                               

来保存我们的工作。当保存的时候vi下方的提示如下所示:

"foo.txt" [New] 1L, 46C written                            1,45          All                      

提示:如果阅读vim的帮助文档的话,你将会看到,命令模式被称为普通模式,而ex 命令将被称为命令模式。我们注意这一点。

3 Moving The Cursor Around  光标定位

在命令模式下,我们可以通过键入一些移动光标的命令来移动光标。常见的操作如下表所示:

key

Moves The Cursor

l or Right Arrow

Right one character.

h or Left Arrow

Left one character

j or Down Arrow

Down one line.

k or Up Arrow

Up one line

0 (zero)

To the beginning of the current line.

^

To the first non-whitespace character on the current line.

$

To the end of the current line

w

To the beginning of the next word or punctuation character

W

To the beginning of the next word or punctuation character.

b

To the beginning of the previous word or punctuation character.

B

To the beginning of the previous word, ignoring punctuation characters.

Ctrl-f or Page Down

Down one page

Ctrl-b or Page Up

Up one page

numberG

To line number.  For example, 1G moves to the first line of the file.

G

To the last line of the file


注意很多的命令都可以以数字开头,通过添加数字我们可以让命令执行多次,比如"5j"就可以将光标向下移动5行。

4.Basic Editing  基本编辑

通常的编辑包括了插入文本、删除文本、剪切和复制文本这些操作。Vi包括了这些操作同时也包含了适当的撤销操作。当在命令模式下按下"u"键的时候,我们就撤销了最后一次的更改。这在编辑时十分方便。

4.1 Appending Text   追加文本

Vi有很多插入的命令。我们已经用过了i命令来进行插入。

这里介绍两个新的命令,aA命令如下表所示:

Command

Append text

a

Append text after the current cursor

A

Append text to the end of the current line

也就是a命令是在当前光标之后插入内容,A命令在当前行末尾插入内容。

下面实例练习一下。

回到上文中的foo.txt.

首先将光标移动到第一行末尾,可以使用"$"命令,然后键入a命令,插入内容后如下:

The qucik brown fox jumped over the lazy dog.It was cool.                                            

这样操作显得不是很快捷,下面使用A命令来插入内容。不保存上述更改,通过"0"命令将光标定位到第一行行首,然后键入A命令,我们即可在行尾直接插入内容,我们插入如下内容:


我们看到A命令直接将光标置到行尾进行插入,十分方便。

4.2 Opening A Line  新增一行

我们通过使用o和 O命令来新增一行。命令如下表所示:

Command

Open a line 

o

Open a blank line  below the current line.

O

Open a blank line  above the current line

也就是使用o命令在当前行的下方插入一行,使用O命令在当前行的上方插入一行。

下面实例演示.

补充:

为了便于观察到效果,这里介绍下让vi显示行号。

在用户主目录,即你登录时的目录 /home/用户名下,使用命令:

qiaoqiao@ubuntu:~$ sudo vim .vimrc                                                  

打开vi配置文件,然后写入内容如下:




来让vi显示行号。

打开foo.txt,将光标定位在第三行,然后键入o命令,我们看到在第三行之下插入一行,如下所示:




撤销刚才的操作,光标依然定位在第三行,使用O命令,我们看到在第三行之上插入一行如下所示:



4.3 Deleting Text   删除文本

Vi提供了很方便的删除文本的命令,x命令删除当前光标处的字符,也可以在x命令钱加上数字来指定删除的字符数目。d命令也用来删除文本,另外,d命令总是跟着一个移动的命令来控制删除的区域。

下表是一些实例:

Command

Deletes text(cuts text)

x

Delete the current character.

3x

Delete the current character and the next two characters

dd

Delete the current line.

5dd

Delete the current line and the next four lines.

dw

Delete from the current cursor position to the beginning of the next word.

d$

Delete from the current cursor location to the end of the current line.

d0

Delete from the current cursor location to the beginning of the line.

d^

Delete from the current cursor location to the first non-whitespace character in the line.

dG

Delete from the current line to the end of the file.

d20G

Delete from the current line to the twentieth line of the file.

下面做一些练习。

打开foo.txt,将光标定位到第一行的"It"单词上,然后重复使用x命令删除这个句子。

按下"u"键撤销操作,这次同样将光标定位在"It"上,我们使用dw命令来删除单词"It",删除后结果如下图所示:




撤销操作,再次将光标定位到"It"上,这次使用d$命令将光标到行尾的部分全部删除,删除后结果如下图所示:



键入dG命令删除第一行到最后一行的内容,删除结果如下图所示:



4.4 Cutting, Copying And Pasting Text  剪切、复制和粘贴


在vi中实际上d命令在删除文本的同时会把删除的内容放到类似于windows下剪切板的缓冲区中,删除的内容可以在以后的操作中用p或者P命令进行粘贴,这类似于剪切操作。

y命令则用来复制内容(称为yank),这与d命令用来剪切内容很相同。下表是一些复制操作的例子:

Command

Copies text

yy

Copy the current line

5yy

Copy the current line and the next four lines.

yW

Copy from the current cursor position to the beginning of the next word

y$

Copy from the current cursor location to the end of the current line.

y0

Copy from the current cursor location to the beginning of the line

y^

Copy from the current cursor location to the first non-whitespace character in the line.

yG

Copy from the current line to the end of the file.

y20G

Copy from the current line to the twentieth line of the file.

下面做一些练习。

打开foo.txt,我们将光标定位在第一行,然后使用yy命令复制第一行内容,再使用G命令将光标置于最后一行,我们使用p命令将内容粘贴在最后一行的下面。

结果如下图所示:



撤销刚才的操作,将光标再次定位到最后一行,这次使用P命令,将内容粘贴在最后一行的上面,结果如下图所示:



操作完毕后请将文件恢复到原状态。

4.5 Joining Lines  行的连接

Vi中对行的概念是很严格的。通常情况下你无法将光标移动到行的末尾来删除行结束符而把下一行连接起来。因此vi中提供了J命令来进行行连接。

将光标置于第三行,我们使用J命令,结果如下图所示:


4.6 Search And Replace 查找和替换

单行查找

单行查找使用f命令,f命令在一行中查找单个字符,并将光标置于下一个字符出现的位置。

例如,我们使用fa命令,在当前行中查找"a"字符。

全文查找

Vi中使用/命令来进行单词或者句子的查找。当我们键入/符号,屏幕下方就会出现/符号。接下来键入单词或者句子,并以回车键结束就开始了全文查找。我们可以使用n命令来重复查找命令,查看下一个单词或者句子出现的地方。下面做一个练习。

使用/Line命令在foo.txt中查找Line单词,键入回车后光标第一次出现在第二行处,接下来键入n时,光标移动到第三行。重复键入n,将会把光标移动到下一个出现Line的位置。

当然除了查找单词和句子外,还可以用正则表达式进行搜素,在这里不做讨论,可以后续学习。

全局查找和替换

Vi使用ex命令来在一定范围内或者整个文件中进行查找和替换。这里我们以将foo.txt中的Line替换为line为例,执行的命令是:

:%s/Line/line/g                                                                       

执行结果如下图所示:



这个命令中各个字段的含义如下表所示:

Item

Meaning

:

The colon character starts an ex command.

%

Specifies the range of lines for the operation.  % is a shortcut 
meaning from the first line to the last line.  Alternately, the 
range could have been specified 1,5 (since our file is five 
lines long), or 1,$ which means “from line 1 to the last line in 
the file.”  If the range of lines is omitted, the operation is onlyperformed on the current line.

s

Specifies the operation.  In this case, substitution (search and 
replace).

/Line/line/

The search pattern and the replacement text

g

This means “global” in the sense that the search and replace is 
performed on every instance of the search string in the line.  If 
omitted, only the first instance of the search string on each line 
is replaced.


我们可以再命令的末尾加上c参数,来提供用户确认的功能。我们执行命令:

:%s/line/Line/gc                                                           

来讲line还原为Line

下图为出现的提示:




屏幕下方出现的替换确认提示符各个字段的含义如下表所示:


Key

Action

y

Perform the substitution.

Skip this instance of the pattern

a

Perform the substitution on this and all subsequent instances of the pattern

q or Esc

Quit substituting.

Perform this substitution and then quit.  Short for “last.”

Ctrl-e, Ctrl-y

Scroll down and scroll up, respectively.  Useful for viewing 
the context of the proposed substitution.


当我们键入y将进行本次的替换,键入n将忽略本次替换并将光标置于下一个待替换的位置,而键入a将执行全部的替换并不再提醒。

4.7 Editing Multiple Files  多文件编辑

一次能编辑多个文件通常是方便的。比如你要从一个文件中拷贝内容到另一个文件。

在vi通过在命令行中指定多个文件名,可以一次打开多个文件。

格式如下:

vi file1 file2 file3 ...                                                                         

为了便于演示,我们首先再建立另外一个文件ls-output.txt,方法如下:

qiaoqiao@ubuntu:~$ ls -l  /usr/bin >ls-output.txt                                                  

下面同时打开两个文件如下:

qiaoqiao@ubuntu:~$ vi foo.txt ls-output.txt                                                       

我们将首先看到foo.txt中的内容。

多文件之间的切换

当打开多个文件后,我们使用:n:N命令来在文件之间切换,:n表示切换到下一个文件,而:N则表示切换回上一个的文件。需要注意的是vi将采取一种策略来防止我们在没有保存当前文件的情况下进行文件切换。要强制进行文件切换我们可以再命令的后面加上感叹号(!)

另外vi还提供了:buffers命令来查看当前编辑文件的列表,我们运行该命令将在屏幕下方看到:




列出了打开的文件信息。要在文件之间切换可以使用:buffer加上你想编辑的文件的号码。例如,你要编辑ls-output.txt,就可以执行:

:buffer 2                                                                         

命令。

打开额外的文件

添加文件到当前编辑会话也是可以的。ex命令:e紧接文件名就可以打开额外的文件。我们先关闭当前会话,重新单独打开foo.txt文件。此时要打开另外一个文件,需要键入:

:e ls-output.txt          

我们可以使用:buffers命令来验证打开了几个文件,验证结果如下图所示:




需要注意的是,利用e命令打开的文件,不能使用:n或者:N命令来切换,而要用:buffer加上数字来切换文件。

从一个文件中拷贝内容到另外一个文件

当编辑多个文件时,我们希望能从一个文件中拷贝一些内容到我们正在编辑的文件中。在vi中可以通过复制命令即可轻松实现。

首先,我们打开上述两个文件,使用命令

:buffer 1         

切换到第一个文件,显示如下图所示:


然后我们在第一行使用yy 命令复制第一行的内容,通过键入

:buffer 2      

切换到第二个文件,第二个文件显示如下:



我们将光标定位在第一行,使用p命令,粘贴内容后如下图所示:


插入一整个文件

Vi中也可以通过命令

:r 加上文件名,向当前编辑的文件中插入文件名指定的文件。

下面来演示。

首先打开一个文件使用命令如下:

qiaoqiao@ubuntu:~$ vi ls-output.txt                                                  

打开文件如下图所示:



在打开的文件中将光标定位在第三行,然后使用命令:

:r foo.txt                                                                       

结果如下图所示:



4.8 Saving Our Work  保存工作

就像vi中的其他操作一样,保存文件的操作也有多种方式。已经讲过了:w命令,还有一些其他命令,也很有用。在命令模式下,键入ZZ可以保存当前编辑的文件并退出vi。同样地使用:wq命令将组合了:w:q命令,既能保存文件又能退出系统。

另外:w可以指定文件名,这样可以实现另存为功能。例如将foo.txt另存为foo1.txt需要键入

:w foo1.txt           

需要注意的是,在当前编辑文件另存为另一个文件后,你编辑的文件文件名不变。也就是你继续编辑的是foo.txt,而不是foo1.txt文件。

总结 

Vi的入门命令学习起来也很简单,下面总结上面的各个命令列在下面供参考学习。

key

Moves The Cursor

l or Right Arrow

Right one character.

h or Left Arrow

Left one character

j or Down Arrow

Down one line.

k or Up Arrow

Up one line

0 (zero)

To the beginning of the current line.

^

To the first non-whitespace character on the current line.

$

To the end of the current line

w

To the beginning of the next word or punctuation character

W

To the beginning of the next word or punctuation character.

b

To the beginning of the previous word or punctuation character.

B

To the beginning of the previous word, ignoring punctuation characters.

Ctrl-f or Page Down

Down one page

Ctrl-b or Page Up

Up one page

numberG

To line number.  For example, 1G moves to the first line of the file.

G

To the last line of the file

Command

Append text

a

Append text after the current cursor

A

Append text to the end of the current line

Command

Open a line 

o

Open a blank line  below the current line.

O

Open a blank line  above the current line

Command

Deletes text(cuts text)

x

Delete the current character.

3x

Delete the current character and the next two characters

dd

Delete the current line.

5dd

Delete the current line and the next four lines.

dw

Delete from the current cursor position to the beginning of the next word.

d$

Delete from the current cursor location to the end of the current line.

d0

Delete from the current cursor location to the beginning of the line.

d^

Delete from the current cursor location to the first non-whitespace character in the line.

dG

Delete from the current line to the end of the file.

d20G

Delete from the current line to the twentieth line of the file.

Command

Copies text

yy

Copy the current line

5yy

Copy the current line and the next four lines.

yW

Copy from the current cursor position to the beginning of the next word

y$

Copy from the current cursor location to the end of the current line.

y0

Copy from the current cursor location to the beginning of the line

y^

Copy from the current cursor location to the first non-whitespace character in the line.

yG

Copy from the current line to the end of the file.

y20G

Copy from the current line to the twentieth line of the file.

Command

Paste text

p

Paste the contents of the buffer after the cursor

P

Paste the contents before the cursor

Command

 Join lines

J

 Join one line with the one below it

Key

Editing Multiple Files

:n 

Switch from one file to the next

:N

Move back to the previous file

:buffers

View a list of files being edited

:buffer number 

Switch to the specified file the number point to 

:e filename

Add files to our current editing session

:r  filename

Inserts the specified file to the current file


更新:

调整vim到适合python格式,在~/.vimrc文件总加入:

set smartindent  set tabstop=4  set shiftwidth=4  set expandtab  即可.

要更快的掌握这些命令需要多加练习,要完全掌握vi的使用,需要学习更多的内容,从事更多的实践。


 本文由用户 JanisV84 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1453712334120.html
Linux