Linux basic command learning-the basic operation of the text editor (seven)

A rookie learns programming techniques, records what he has learned and shares it with everyone. I hope everyone can support me.

1. Basic operation of command mode

After entering the vi command, you enter the full-screen editing environment, and the state at this time is 命令模式.
In the command mode, enter some commands, but when you click the corresponding key on the keyboard to enter the commands, these commands will show the final effect, and the command characters will not be displayed on the editor interface.

1. Basic operation of vim editor

Subcommand name effect
i Edit the document and display it in the status line – INSERT
w Save the edited document
q Exit to close document
q! Force close document exit
/ String Find the specified string in the document
set no The line number is displayed in the document
set nonu Line numbers are not displayed in the document
d Delete the current line
nd Delete from current line to n lines
n1, n2d Delete line n1 to line n2
s / string1 / string2 / g Replace string 1 with string 2 in the document
ESC Exit document editing state and enter non-editing state
in In the non-editing state, undo the last operation

2. Quick jump in the line

Operation keys Features
^ Quickly jump the cursor to the first character of the line home
$ Quickly jump to the end of the line
w Quickly jump the cursor to the first letter of the next word at the current cursor position
b Quickly jump the cursor to the first letter of the previous word at the current cursor position
e Quickly jump the cursor to the last letter of the next word at the current cursor position
Arrow keys Move the cursor up, down, left, and right
Home Quickly position the cursor to the beginning of the line
End Quickly position the cursor to the end of the line

3. Quick jump between lines

command Features
: set no Show line number in editor
:set nonu Cancel line number display in the editor
1G Jump to the first line of the file
G Jump to the end of the file
#G Jump to line # of the file
PageUp Page up text
PageDown Page down

Second, the deletion and revocation of vim editor

1. Deletion of file content

command Features
x Delete a single character at the cursor = delete
dd Delete the line where the cursor is located ndd can delete 5 lines of content
dw Delete all characters from the current character to the end of the word (including spaces)
of Delete all characters from the current character to the end of the word (excluding spaces at the end of the word)
d$ 删除当前字符到行尾的所有字符
d^ 删除当前字符到行首的所有字符
J 删除光标所在行行尾的换位符,相当于合并当前行和下一行的内容

2、文件内容的撤销

命令 功能
u 取消最近一次的操作,并恢复操作结果(可以多次使用u命令恢复已进行的多步操作)
U 取消对当前行进行的所有操作
Ctrl+r 对使用u命令撤销的操作进行恢复

三、文件内容的复制和粘贴

单行复制:在命令模式下,将光标移动到将要复制的行处,按“yy”进行复制;
多行复制:在命令模式下,将光标移动到将要复制的首行处,按“nyy”复制n行,其中n为数字;
粘贴:在命令模式下,将光标移动到将要粘贴的行处,按“p”进行粘贴。

命令 功能
yy 复制当前行整行的内容到vi缓冲区,5yy从当前行开始复制5行
yw 复制当前光标到单词尾字符的内容到vi缓冲区
y$ 复制当前光标到行尾的内容到vi缓冲区
y^ 复制当前光标到行首的内容的vi缓冲区
p 读取vi缓冲区中的内容,并粘贴到光标当前的位置(不覆盖文件已有的内容)

四、vim编辑器的查找与替换

1、文件内容的查找

vi提供了几种定位查找一个指定的字符串在文件中位置的方法。同时还提供一种功能强大的全局替换功能。
为查找一个字符串,在vi命令模式下键入“/”,后面跟要查找的字符串,再按回车。
vi将光标定位在该串下一次出现的地方上。键入“n”跳到该串的下一个出现处,键入“N”跳到该串的上一个出现处。

命令 功能
/word 从上而下载文件中查找字符串“word”
?word 从下而上在文件中查找字符串“word”
n 定位下一个匹配的被查找字符串
N 定位上一个匹配的被查找的字符串

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命令,将对每个替换动作提示用户进行确认
发布了19 篇原创文章 · 获赞 211 · 访问量 1万+

Guess you like

Origin blog.csdn.net/qq_44723773/article/details/105520291