Linux学习之——文本编辑器 vi/vim(七)

 

●Linux提供了一系列功能强大的编辑器,如vi和Emacs。vi 是linux系统的第-一个全屏幕交互式编辑器,从诞生到现在一-直得到广大用户青睐。

●vim是vi的强化版本,完全兼容vi操作。

●vim的一般使用方法:

[root@bogon ~]#vim filepath

vim有3种工作模式,分别是命令行模式、编辑模式、最底行模式。

文本编辑的三种模式

命令行模式:

最初进入vi之后的模式,该模式下可以移动光标进行浏览,整行删除,但无法输入文字。

编辑模式:

只有在该模式下,用户才能进行文字的编辑输入。i 或 a 键进入该模式,退出用ESc键

最底行模式:

该模式下,光标位于屏幕底行,用户可以进行文件保存或退出操作,也可以设置编辑环境,如寻找字符串、列出行号。

在命令行模式下输入":"进入 最底行模式 输入 "/" 可以查找

文本编辑:命令行模式功能键

yy 复制当前光标所在行
[n]yy n为数字,复制当前光标开始的n行
p 粘贴复制的内容到光标所在行之下,小写的p是粘贴到光标所在行的后一行,大写的P是粘贴到光标所在行的前一行
dd 删除当前光标所在行
[n]dd 删除当前光标所在行开始的n行
cc 剪切当前光标所在行( 会进入编辑模式)
[n]cc n为数字,剪切当前光标开始的n行
G 光标移动到文件尾(注意是大写)
u 取消前一一个动作(注意是小写)
. 重复前一个动作
X 删除光标当前的一个字符
ZZ 保存并退出
ddp 光标所在行跟下面一行进行交换
xp 光标所在字符和后一个字符交换

在VIM中,进入编辑模式的方式有很多:i、I、a、A、o、O

o 小写,在当前行的下面插入一行,并进入INSERT模式
O 大写,在当前行的上面插入一行,并进入编辑模式
i 在当前位置进入编辑模式
I 从行首开始进入编辑模式
a 在当前位置后一个字符插入,进入编辑模式
A 在行尾最后一个字符后面插入,进入编辑模式

文本编辑:底行模式功能键

:w 保存退出vi(系统会提示保存修改)
:q! 强行退出(对修改不做保存)
:wq 保存后退出
:w [filepath] 另存文件到filepath
:set nu 显示行号
:n 定位到第n行
:set nonu 取消行号
:n1,n2 co n3 将n1到n2行所有文本复制到n3行之下
:n1,n2 mv 3 将n1到n2行所有文本移动到n3行之下
:n1,n2 d 删除n1到n2行的所有文本
/ name 查找光标之后的名为“name" 的字符串,如果查找下一个,按“n”即可,在查找时,忽略关键字的大小可用\c选项。 例如: / conf \c //在文本中查找conf 字符串,忽略大小写
: s/str1/str2/ 将当前行的第一个字符串str1替换为字符串str2
: %s/str1/str2/g 将所有行的字符串str1替换为字符串str2

课后练习

1.使用vi/vim编辑器进行文档处理:

用vi打开一个新文档,输入以下内容:

Hooray! It's snowing! It's time to make a SnowMan.

He puts a big snowball on top.

James runs out. He makes a big pile of snow.

He adds a scarf and a hat.

He adds an orange for the nose.

He adds coal for the eyes and buttons.

In the evening, James opens the door.

What does he see? The SnowMan is moving!

James invites him in.

The SnowMan has never been inside a house.

He says hello to the cat. He plays with paper towels.

完成以下操作:

注意:以下行号均表示当前行号。

(1)显示行号。

1 Hooray! It's snowing! It's time to make a SnowMan.
2 He puts a big snowball on top. 
3 James runs out. He makes a big pile of snow. 
4 He adds a scarf and a hat. 
5 He adds an orange for the nose. 
6 He adds coal for the eyes and buttons.
7 In the evening, James opens the door. 
8 What does he see? The SnowMan is moving! 
9 James invites him in. 
10 The SnowMan has never been inside a house. 
11 He says hello to the cat. He plays with paper towels.
12
​
:set nu

(2)保存到文件/root/snowman.txt,不要退出。

 1 Hooray! It's snowing! It's time to make a SnowMan.
 2 He puts a big snowball on top. 
 3 James runs out. He makes a big pile of snow. 
 4 He adds a scarf and a hat. 
 5 He adds an orange for the nose. 
 6 He adds coal for the eyes and buttons.
 7 In the evening, James opens the door. 
 8 What does he see? The SnowMan is moving! 
 9 James invites him in. 
10 The SnowMan has never been inside a house. 
11 He says hello to the cat. He plays with paper towels.
12
​
​
:w /root/snowman.txt
"/snowman.txt"[new] 12L,421C written

(3)查找单词“the”(忽略大小写)

/the \c

(4)把第2段和第3段换行。

 ddp     //光标所在行跟下面一行进行交换
 1 Hooray! It's snowing! It's time to make a SnowMan.
 2 James runs out. He makes a big pile of snow.
 3 He puts a big snowball on top. 
 4 He adds a scarf and a hat. 
 5 He adds an orange for the nose. 
 6 He adds coal for the eyes and buttons.
 7 In the evening, James opens the door. 
 8 What does he see? The SnowMan is moving! 
 9 James invites him in. 
10 The SnowMan has never been inside a house. 
11 He says hello to the cat. He plays with paper towels.
12

(5)将第4段的内容复制到文档的最后。

 1 Hooray! It's snowing! It's time to make a SnowMan.
 2 James runs out. He makes a big pile of snow.
 3 He puts a big snowball on top. 
 4 He adds a scarf and a hat. 
 5 He adds an orange for the nose. 
 6 He adds coal for the eyes and buttons.
 7 In the evening, James opens the door. 
 8 What does he see? The SnowMan is moving! 
 9 James invites him in. 
10 The SnowMan has never been inside a house. 
11 He says hello to the cat. He plays with paper towels.
12 He adds a scarf and a hat. 
13
​
​
:4 copy 11

(6)删除第5段的内容。

dd //删除光标所在行
 1 Hooray! It's snowing! It's time to make a SnowMan.
 2 James runs out. He makes a big pile of snow.
 3 He puts a big snowball on top. 
 4 He adds a scarf and a hat. 
 
 5 He adds coal for the eyes and buttons.
 6 In the evening, James opens the door. 
 7 What does he see? The SnowMan is moving! 
 8 James invites him in. 
 9 The SnowMan has never been inside a house. 
10 He says hello to the cat. He plays with paper towels.
11 He adds a scarf and a hat. 
12
​

(7)恢复被删除的一段内容。

按 "u"键  //撤销上一次操作
​
 1 Hooray! It's snowing! It's time to make a SnowMan.
 2 James runs out. He makes a big pile of snow.
 3 He puts a big snowball on top. 
 4 He adds a scarf and a hat. 
 5 He adds an orange for the nose. 
 6 He adds coal for the eyes and buttons.
 7 In the evening, James opens the door. 
 8 What does he see? The SnowMan is moving! 
 9 James invites him in. 
10 The SnowMan has never been inside a house. 
11 He says hello to the cat. He plays with paper towels.
12 He adds a scarf and a hat. 
13

(8)查找所有的“SnowMan”单词,并全部改为“snowman”。

 1 Hooray! It's snowing! It's time to make a snowman.
 2 James runs out. He makes a big pile of snow.
 3 He puts a big snowball on top. 
 4 He adds a scarf and a hat. 
 5 He adds an orange for the nose. 
 6 He adds coal for the eyes and buttons.
 7 In the evening, James opens the door. 
 8 What does he see? The snowman is moving! 
 9 James invites him in. 
10 The snowman has never been inside a house. 
11 He says hello to the cat. He plays with paper towels.
12 He adds a scarf and a hat. 
13
​
​
/SnowMan 
:%s/SnowMan/snowman/g
3 substitutions on 3 lines

(9)保存修改,退出vi。

:wq

(10)使用vi再次打开文件 /root/snowman.txt,在第1段前插入自己的学号和姓名全拼,例如“09123456xiaozhang"。

[root@bogon ~]# vi /root/snowman.txt
 "O"  //大写,在当前行的上面插入一行,并进入INSERT模式。
  1 012345678XiaoMing
  2 Hooray! It's snowing! It's time to make a SnowMan.
  3 He puts a big snowball on top. 
  4 James runs out. He makes a big pile of snow. 
  5 He adds a scarf and a hat. 
  6 He adds an orange for the nose. 
  7 He adds coal for the eyes and buttons.
  8 In the evening, James opens the door. 
  9 What does he see? The SnowMan is moving! 
 10 James invites him in. 
 11 The SnowMan has never been inside a house. 
 12 He says hello to the cat. He plays with paper towels.
 13
(11)将第3行到第6行的内容复制到第10行之下。
  1 012345678XiaoMing
  2 Hooray! It's snowing! It's time to make a SnowMan.
  3 He puts a big snowball on top. 
  4 James runs out. He makes a big pile of snow. 
  5 He adds a scarf and a hat. 
  6 He adds an orange for the nose. 
  7 He adds coal for the eyes and buttons.
  8 In the evening, James opens the door. 
  9 What does he see? The SnowMan is moving! 
 10 James invites him in. 
 11 He puts a big snowball on top. 
 12 James runs out. He makes a big pile of snow. 
 13 He adds a scarf and a hat. 
 14 He adds an orange for the nose. 
 15 The SnowMan has never been inside a house. 
 16 He says hello to the cat. He plays with paper towels.
 17
​
​
:3,6 copy 10
4 more lines

(12)将第5行到第7行的内容移动到第2行之下。

  1 012345678XiaoMing
  2 Hooray! It's snowing! It's time to make a SnowMan.
  3 He adds a scarf and a hat. 
  4 He adds an orange for the nose. 
  5 He adds coal for the eyes and buttons.
  6 He puts a big snowball on top. 
  7 James runs out. He makes a big pile of snow. 
  8 In the evening, James opens the door. 
  9 What does he see? The SnowMan is moving! 
 10 James invites him in. 
 11 He puts a big snowball on top. 
 12 James runs out. He makes a big pile of snow. 
 13 He adds a scarf and a hat. 
 14 He adds an orange for the nose. 
 15 The SnowMan has never been inside a house. 
 16 He says hello to the cat. He plays with paper towels.
 17
​
​
​
:5,7 move 2
3 lines moved

(13)保存并退出vi。

:wq

猜你喜欢

转载自blog.csdn.net/xiaohaiguang/article/details/105168339
今日推荐