Linux Basics learning --vi, vim interactive text editing tools

Linux text editor

vi, vim interactive text editing tools

Vi (Visual interface Visual Interface )
Vim is an enhanced version of Vi (Visual interface Improved)
system comes Tutorial: vimtutor

sed stream processing non-interactive text editor
sed -i 's / b / a / g' filname the text to a ignore case b

linux text processing tools vi, vim, Emacs a lot, why use vi, vim?
  1. Class UNXI system built vi text editor
  2. procedure call (the crontab, the visudo, the edquota)
  3. ability to edit a program, the color discrimination on the grammatical correctness
  4. The procedure is simple and fast

Switching between modes and mode vim

Command mode, input mode, line mode
command mode -----> input mode i,I,O,o,A,a
command mode -----> line mode
input / last line mode -----> command modeEsc

Command Mode

operating Operation keys Features
Cursor direction movement k or ↑ on
j or ↓ under
h or ← left
or l → right
Next page Page Down 或 Ctrl+F On a turn
Page Up 或 Ctrl+B Down one page
Quick Jump in the line Home or 0 Jump to the absolute beginning of the line
^ Go to the heading of the first non-blank character
Or $ End Jump to the absolute end of the line
Quick jump between the lines 1G or gg Jump to the first line of the file
G Go to the end of the last line of the file
#G Jump to file the first line #
delete x or Delete To delete a single character at the cursor
dd Delete the current cursor row
#dd # Delete lines that start from point
d^ Delete all the characters at the current cursor to the beginning of the line
d$ Delete the current cursor position to the end of the line all characters
copy yy Copy the entire line current line into the clipboard
#yy # Copy the contents of the line starting from the cursor
Stick p After pasting the cursor
P Before the cursor Paste
Inquire /word Top-down search string word
?word Bottom-word search string
n Consistent with the direction of the query
N Opposite to the direction of the query
Save and exit zz

Input Mode

Features command Remark
Insert (insert) i Inserted in front of the cursor position
I In the first row of the cursor line insertion (if there are spaces in the first row is inserted after the space)
Add (append) a After the new location of the cursor
A (End of line if a space is inserted after the space) in the line end of the line the cursor new
Start (open) O In the following line the cursor is restarted row
O Re-start line in the top row of the cursor

Line mode

Features command Remark
save document :w
:w! Force Save
:w /tmp/newfile Save as another file
drop out :q
:q! Abandon the file contents Force Quit
Save and Exit :wq
:wq! Forced to write and quit
Display line numbers : Not set Show Line Numbers
:set nonu Cancel Display line numbers
Open other files :e /tmp/filename Open the file for editing
:r /etc/passwd In the current file read other files
replace : s /old/new The current row find the first old replaced with new
: s /old/new/g The current row to find all the old replaced by new
:2,5 s /old/new The first old line 2-5 replaced new
:2,5 s /old/new/g 2-5 all old row replace new
:% s /old/new/ The full text of the old replaced by new
: s /old/new/c Tips for the user to confirm each replacement operation

1. Open the file

vim open position the cursor when the cursor position to the last exit
to open the file at the first # line vim +# filename
to open the file located in the last row and vim + filename
open the file, navigate to the first matching PATTERN to be the first line of the line vim +/PATTERN filename from the last cursor location start match

2. Close the file

mode command Remark
Line mode :q drop out
:wq Save and exit
q! Force Quit
w Storage
:w! Force Save
:wq! Forced to save and exit
命令模式 ZZ 保存退出

3.移动光标

命令模式

操作 命令 备注
逐字符移动 k 或 ↑
j 或 ↓
h 或 ←
l 或 →
以单词为单位移动 w 移到下一个单词的词首
#w 移动#个单词
e 跳至当前或下一个单词的词尾
b 跳至当前或上一个单词的词首
行内跳转 0 绝对行首
^ 行首第一个非空白字符
$ 绝对行尾
行间跳转 #G 跳转到第#行
G 最后一行

末行模式

操作 命令
行间跳转 直接输入行号,Enter

4.翻屏

模式 命令 备注
命令模式 Page Down 或 Ctrl+F 上翻一屏
Page Up 或 Ctrl+B 下翻一屏

5.删除

命令模式

操作 命令 备注
删除单个字符 x 或 Delete 删除光标所在处的单个字符
#x 删除光标所在处及向后共#个字符
删除命令 dd 删除当前光标所在行
#dd 删除从光标处开始的#行
d^ 删除当前光标处到行首的所有字符
d$ 删除当前光标处到行尾的所有字符
删除d和跳转w组合 dw 删除光标所在位置到下一个单词词首所有字符
#dw 删除当前光标所在处及向后第#个单词词首所有字符
删除d和跳转e组合 de 删除光标所在位置到下一个单词词尾所有字符
#de 删除当前光标所在处及向后第#个单词词尾所有字符
删除d和跳转b组合 db 删除光标所在位置到当前单词或上一个单词词首所有字符
#db 删除当前光标所在处及向前第#个单词词首所有字符

6.粘贴

p 粘贴到光标所在行下方
P 粘贴到光标所在行上方
如果复制或删除的内容为非整行,则粘贴至光标所在字符的后面p或前面P

7.复制

命令模式

yy 复制当前行
yw (y#w,ye,y#e,yb,y#b) 复制光标当前位置到下个单词词首内容
y#G 复制当前位置当#行处的所有内容

末行模式

StartADD,EndADDy 复制StartADD到EndADDd位置的所有行

:10,15d 复制10到15行所有内容
:+#y 复制当前位置向下#行的行内容

8.修改

先删除再输入

9.替换

r 单字符替换,会替换光标选中字符
R 替换模式,可以做字符替换

10.撤销操作

u 撤销前一次编辑操作
#u 直接撤销#次操作
Ctrl+r 撤销最近一次撤销操作

11.重复前一次编辑操作

. 重复前一次编辑操作
比如上一次dd删除了一行,现在输入.就可以重复dd删除行的操作

12.可视化操作

命令模式

v 按字符选取,键盘控制光标划过的字符被选取
V 按行选取,键盘控制光标划过的行被选取
Ctrl+v 按块选取,键盘控制光标划过的块被选取(结合Backspace使用)

13.查找

末行模式

/PATTERN 从当前位置向后查找
?PATTERN 从当前位置向前查找
n 向下查找
N 向上查找

14.查找并替换

在末行模式下使用s命令
% 全文 1,$也表示全文
g 表示全局替换
i 表示忽略大小写

功能 命令 备注
替换 : s /old/new 将当前行中查找到第一个old替换为new
: s /old/new/g 将当前行中查找到所有old替换为new
:2,5 s /old/new 将第2-5行的第一个old替换为new
:2,5 s /old/new/g 将第2-5行的所有old替换为new
:% s /old/new/g 将全文的old替换为new
: s /old/new/c 对每个替换动作提示用户确认

15.与shell交互

末行模式

:!command
:! ls /etc/ 会列出/ect目录下所有文件及目录,按enter后回到vim编辑状态

16.高级操作

末行模式

功能 命令 备注
显示或取消行号 :set nu 显示行号
:set nonu 取消显示行号
显示忽略或区分字符大小写 set ic 忽略大小写
set noic 区分大小写
设定自动缩进 set ai 自动缩进
set noai 取消自动缩进
查找到的文本高亮或取消高亮显示 set hlsearch 高亮显示搜索到的文本
set nohlsearch 取消高亮显示搜索到的文本
语法高亮 syntax on 打开语法高亮显示
syntax off 关闭语法高亮显示

17.vim配置文件

全局vim配置文件 /etc/vimrc
个人vim配置文件 /root/.vimrc
vim末行模式使用过的命令保存位置 /root/.viminfo

[root@libaoliang ~]# echo "set nu" >> /root/.vimrc	;root用户用vim打开文件时,显示行号

练习

1.向后搜索字符串stream /stream
2.向前搜索字符串uplook ?uplook
3.复制10行然后粘贴 10yy --->p
4.将光标移到第55行 55G
5.将光标移到最末行 G
6.删除共20行 20dd
7.恢复前一步的操作 u
8.将光标移到行首 Home 或 0 或 ^
9.将光标移到行尾 End 或 $
10.将从第10行到第50行的内容写入/back/file1 :10,50w /back/file1
11.将从当前行开始,总共20行的内容存入/back/file2 :.,+19w /back/file2
12.读取/etc/conf/hello.txt文件的内容插入当前行后面 :r /etc/conf/hello.txt
13.替换第20行到第80行中的所有httpd为bind :20,80 s/httpd/bind/g
14.替换文中全部的root为admin :% s/root/admin/g
15.在每一行的行首插入字符串new :% s/^/new/
16.在第10行到第50行的行尾插入字符串new :10,50 s/$/new/
17.让vi显示每一行的行号 :set nu

发布了43 篇原创文章 · 获赞 30 · 访问量 5940

Guess you like

Origin blog.csdn.net/qq_42049496/article/details/102754686