1-5 vi文本编辑器

2016.6.2



一、三种工作模式概述

1.文本编辑器的作用

  • 创建或修改文本文件

  • 维护Linux系统中的各种配置文件

2.Linux中最常用的文本编辑器

  • vi:类Unix系统中的默认的文本编辑器

  • vim:vi编辑器的增强版本,习惯上也成为vi

3.三种工作模式

  • 命令行模式

  • 输入模式

  • 末行模式


vi-3-modes




二、命令模式的常用操作

1.光标移动

操作类型 操作键 功能
方向移动 ↑、↓、←、→ 上、下、左、右
翻页 Page Down或Ctrl + F 向下翻动一整页内容
Page Up或Ctrl + B 向上翻动一整夜内容
行内快速跳转 Home键或“^”,数字“0” 跳转至行首
End键或“$"键——跳转至行尾 跳转至行尾
行间快速跳转 1G 或者gg 跳转至文件的首行
G 跳转到文件的末尾行
#G 跳转到文件的第#号

2.复制、粘贴、删除:

删除

  • x或Del——删除光标处的单个字符

  • dd——删除当前光标所在行

  • #dd——删除光标所在开始的#行内容

  • d^——删除光标之前到行首的所有字符

  • d$——删除当前光标处到行尾的所有字符

复制

  • yy——复制当前行整行的内容到剪贴板

  • #yy——复制从光标所在行开始的#行内容

粘贴

  • p——将缓冲区中的内容粘贴到光标位置处之后

  • P——粘贴到光标位置之后

文件内容查找:

|-- | -- | |/word | 从上而下在文件中查找字符串“word”| |?word | 从下而上在文件中查找字符串“word"| |n |定位下一个匹配的被查找字符串| |N |定位上一个匹配的被查找字符串|




三、末行模式的常用操作

1.打开新文件或读入其他文件内容

命令 功能
e 打开新的文件进行编辑
r 在当前文件中读入其他文件内容

2.文件内容替换

命令 功能
:s/old/new 将当前行中查找到的第一个字符“old”串替换为“new”
:s/old/new/g 将当前行中查找到的所有字符串“old”替换为“new”
:#,# s/old/new/g 在行号“#,#”范围内替换所有的字符串“old"为"new”
:% s/old/new/g 在整个文件范围内替换所有的字符串“old”为“new”
:s /old/new/c 在替换命令末尾加入c命令,将对每个替换动作提示用户进行确认。

3.显示行号

命令 作用
:set nu 在编辑器中显示行号
:set nonu 取消编辑器中的行号显示


Summary of Basic vi Commands

Command Meaning
Starting vi
vi filename Open or create file
vi Open new file to be named later
vi-r filename Recover crashed file
viewfilename Open file read-only
Cursor Commands
h Move left one character
j Move down one line
k Move up one line
l Move right one character
w Move right one word
W Move right one word (past punctuation)
b Move left one word
B Move left one word (past punctuation)
e Move to end of current word
Return Move down one line
Backspace Move left one character
Spacebar Move right one character
H Move to top of screen
M Move to middle of screen
L Move to bottom of screen
Ctrl-F Page forward one screen
Ctrl-D Scroll forward one-half screen
Ctrl-B Page backward one screen
Ctrl-U Scroll backward one-half screen
Inserting Characters and Lines
a Insert characters to right of cursor
A Insert characters at end of line
i Insert characters to left of cursor
I Insert characters at beginning of line
o Insert line below cursor
O Insert line above cursor
Changing Text
cw Change word (or part of word) to right of cursor
cc Change line
C Change from cursor to end of line
s Substitute string for character(s) from cursor forward
r Replace character at cursor with one other character
rReturn Break line
J Join current line and line below
xp Transpose character at cursor and character to right
~ Change case of letter (upper or lower)
u Undo previous command
U Undo all changes to current line
:u Undo previous last-line command
Deleting Text
x Delete character at the cursor
X Delete character to the left of the cursor
dw Delete word (or part of word to right of cursor)
dd Delete line containing the cursor
D Delete part of line to right of cursor
dG Delete to end of file
d1G Delete from beginning of file to cursor
:5,10 d Delete lines 5-10
Copying and Moving Text
yy Yank or copy line
Y Yank or copy line
p Put yanked or deleted line below current line
P Put yanked or deleted line above current line
:1,2 co 3 Copy lines 1-2 and put after line 3
:4,5 m 6 Move lines 4-5 and put after line 6
Setting Line Numbers
:set nu Show line numbers
:set nonu Hide line numbers
:set ic Searches should ignore case
:set noic Searches should be case-sensitive
Finding a Line
G Go to last line of file
1G Go to first line of file
21G Go to line 21
Searching and Replacing
/string Search for string
?string Search backward for string
n Find next occurrence of string in search direction
N Find previous occurrence of string in search direction
:g/search/s//replace/g Search and replace
Clearing the Screen
Ctrl-L Clear (refresh) scrambled screen
Inserting a File into a File
:r filename Insert (read) file after cursor
:34r filename Insert file after line 34
Saving and Quitting
:w Save changes (write buffer)
:w filename Write buffer to named file
:wq Save changes and quit vi
ZZ Save changes and quit vi
:q! Quit without saving changes


man vi


results matching ""

    No results matching ""