Summary of quick use of Linux editor vi/vim

1. Introduction

As the most commonly used and classic editor in Unix-like systems, vim/vi (vim is developed from vi) provides programmers with rich editing functions, and can greatly improve the efficiency of editing after skilled use. Vim takes a lot of time to learn for developers who are just using it. Although the official vim also gives a variety of pictures for easy memory, vim operation still requires continuous use and proficiency in order to master this tool. Really appreciate its efficiency and convenience.

This article briefly summarizes the basic operations commonly used in vim for viewing when needed. Some official memory maps are as follows:
Please add a picture description

insert image description here

Please add a picture description

2. vi/vim editing mode

Vi/vim editing is usually divided into three modes: Command mode, Insert mode and Last line mode. Characters in different modes have different meanings:

1. Command mode:

After vi/vim is started, it first enters the command mode. The characters entered in this mode will be recognized as commands and matched with the commands for execution. The commands used in this mode can be executed directly without the Enter key. For example, enter i to enter directly. edit mode.

2. Input mode:

Enter the input mode from the command mode through i or other commands. In this mode, the text can be edited and modified. The ESC key can exit the input mode and return to the command mode.

3. Bottom line command mode:

By: or other commands, you can enter the bottom line command mode from the command mode. In this mode, you can enter a single or multiple character commands, press the Enter key to execute, and automatically return to the command mode after execution; in the bottom line command mode, you can press the ESC key at any time Exit to return to command mode.

insert image description here

3. vi/vim command description

1. Command mode:

  • Cursor movement:
button Function
h or ← Move the cursor one character to the left
j or ↓ Move the cursor down one character
k or ↑ Move the cursor up one character
l or→ Move the cursor one character to the right
[num] + hjkl (or ←↓ ↑→) The cursor moves num characters in a certain direction (for example, 30j or 30↓ can mean moving down 30 lines)
[ctrl] + f Move the screen down one page
[ctrl] + b Move the screen up one page
[ctrl] + d Move the screen half a page down
[ctrl] + u Move the screen half a page up
+ Move the cursor to the next line that is not a space character
- Move the cursor to the line above a non-space character
0 or [Home] Move to the first character of the line where the cursor is located
$ or [End] Move to the last character of the line where the cursor is located
gg The cursor moves to the first line of the document, which is equivalent to 1G
H The cursor moves to the first character on the top line of the screen
M The cursor moves to the first character of the line in the center of the screen
L The cursor moves to the first character of the bottom line of the screen where it is located
G Move the cursor to the last line of the document
[num] + G Move the cursor to the num line of the document (for example, 20G will move the 20th line of the document)
[number] + [Enter] Move the cursor down num lines (for example, enter 20 and press the Enter key [Enter], the cursor moves down 20 lines)
[num] + [space] Move the cursor to the right by num characters (for example, press the space bar [space] after entering 20, and the cursor moves backward by 20 characters)
  • copy and paste:
button Function
x cut one character backwards
X cut one character forward
[num] + x Delete n characters backward (for example, 20x will delete 20 characters backward)
p Paste the copied data on the line below the cursor
P Paste the copied data on the line above the cursor
J Combine the row where the cursor is located with the data of the next row into one row
[num] + cc/cj Delete num lines downward (for example, 20cc or 20cj will delete 20 lines downward, note: enter the input mode after deletion)
dd Cut the line where the cursor is
[num] + dd Cut num lines down (for example, 20dd will cut 20 lines down)
dG Delete everything from the line where the cursor is to the last line
d1G Delete everything from the line where the cursor is to the first line
d$ Delete everything from the line where the cursor is to the last character
d0 Delete everything from the line where the cursor is to the first character
yy Copy the line where the cursor is
[num] + yy Copy num rows down (for example, 20yy will copy 20 rows down)
yG Copy everything from the line where the cursor is to the last line
y1G Copy everything from the line where the cursor is to the first line
y$ Copy everything from the line where the cursor is to the last character
y0 Copy everything from the line where the cursor is to the first character
. repeat previous operation
u repeat previous operation
[ctrl] + r redo last action
ZZ Save and force quit the document
ZQ Do not save and force quit the document
  • Search and replace:
button Function
n Repeat last search operation
N Reverse the last search operation
/word 向光标之下寻找一个名称为 word 的字符串(例/cascatrix会向下寻找名为cascatrix字符串)
?word 向光标之上寻找一个名称为 word 的字符串(例/cascatrix会向上寻找名为cascatrix字符串)
:n1,n2s/word1/word2/g 在第 n1 与 n2 行之间寻找 word1 这个字符串并将该字符串取代为 word2(例:1,$s/carson/cascatrix/g 或 :%s/carson/cascatrix/g会从第一行到最后一行寻找名为carson的字符串并将该字符串取代为cascatrix)
:n1,n2s/word1/word2/gc 在第 n1 与 n2 行之间寻找 word1 这个字符串并将该字符串取代为 word2,取代前显示是否需要取代的确认提示(例:1,$s/carson/cascatrix/gc 或 :%s/carson/cascatrix/gc会从第一行到最后一行寻找名为carson的字符串并将该字符串取代为cascatrix,在取代前进行提示:是否需要取代)

2. 输入模式:

命令模式与输入模式之间切换:

按键 功能
i 从当前光标所在位置开始输入
I 从当前光标所在行的第一个非空格符号处开始输入
a 从当前光标所在位置的下一个字符处开始输入
A 从当前光标所在行的最后一个字符处开始输入
o 从当前光标所在位置的下一行输入新的一行
O 从当前光标所在位置的上一行输入新的一行
r 取代当前光标所在位置的那一个字符一次
R 取代当前光标所在位置的那一个字符,直到按下ESC退出替换
[ESC] 退出输入模式,返回命令模式

输入模式编辑操作:

按键 功能
[END] 移动光标到行尾
[Home] 移动光标到行首
[Enter] 换行
[Delete] 删除光标后一个字符
[Backspace] 删除光标前一个字符
[Page Up] 向上翻页
[Page Down] 向下翻页
[Insert] 切换光标为输入/替换模式,光标将变成竖线/下划线
方向键 移动光标
字符按键 输入字符

3. 底线命令模式:

按键 功能
:w 保存文档
:q 退出文档
:w! Force save the document
:q! Force quit document
:wq Save and exit the document
:! command Temporarily leave the vi interface and go to the command line mode to execute the command to display the results
:r [filename] Read the data of another document in the edited data, and add the content of the document filename after the row where the cursor is located
:w [filename] Save the edited data as another file named filename
:n1,n2 w [filename] Store the content of n1 to n2 as filename this document
:set no Display the line number. After setting, the line number of the line will be displayed in the prefix of each line
:set nine Cancel line number, cancel the displayed line number

Guess you like

Origin blog.csdn.net/weixin_43361652/article/details/128205349