use of vim
提示:以下是本篇文章正文内容,下面案例可供参考
1. vim
1.1 A brief introduction to vim
vi
Is the abbreviation of "visual interface", its status on Linux is like Notepad in Windows. It can perform many text operations such as editing, deleting, finding, replacing, block operations, etc., and users can edit them according to their own needs. Customize.. vi
is a text editing program with no menus, only commands. vim
is vi
a premium version of .
1.2 Three modes of vim
Vi
There are three basic working modes: command mode, text input mode, and last line mode.
1.3 Basic operation of vim
1.3.1 Operation in Command Mode
The user presses the esc
key to vi
enter command mode; when using vi
to open a new file, it also enters command mode.
save and exit
hot key | operate |
---|---|
ZZ | save and exit |
When you use Vi / Vim to edit, in command line mode, you use ZZ to quit the following situation (this is what I tested with Centos7.6)
code formatting
hot key | operate |
---|---|
gg=G | formatting of code |
cursor movement
hot key | operate |
---|---|
h | cursor left |
j | cursor down |
k | code up |
l | cursor right |
w | move a word |
gg | Move the cursor to the beginning of the file |
G | Move the cursor to the end of the file |
0 | Move the cursor to the beginning of the line |
$ | Move the cursor to the end of the line |
nG | Line jump, example 12G, jump to line 12 |
delete command
hot key | operate |
---|---|
x |
Delete a character after the cursor, equivalent to Del |
X |
Delete the character before the cursor, equivalent to Backspace |
dw |
Delete the word at the starting position of the cursor, including the character where the cursor is located |
d0 |
Delete all the content of the line before the cursor, excluding the character where the cursor is located |
D[d$] |
Delete all the content of the line after the cursor, including the character where the cursor is located |
dd |
Delete the line where the cursor is located (essentially cut) |
ndd |
Delete the specified number of lines down from the current line of the cursor, such as 15dd |
v/ctrl+v |
Use h, j, k, l to move the selection, then press d to delete where ctrl+v is column mode and v is non-column mode |
Revocation and Anti-Revocation Orders
hot key | operate |
---|---|
u |
Undo step by step, equivalent to ctrl+z in a word document |
ctrl-r |
Anti-undo, equivalent to ctrl+y in word document |
copy and paste
hot key | operate |
---|---|
yy |
copy current line |
nyy |
copy n lines, such as10yy |
P |
Open a new line down at the cursor position and paste |
p |
Open a new line down at the cursor position and paste |
cut operation | Press dd or ndd delete to save the deleted line to the clipboard, then press p/P to paste |
Visual mode
hot key | operate |
---|---|
v/ctrl+v |
Use h、j、k、l move to select content; use d delete , use y copy, use p paste to the back of the cursor, use P to paste to the front of the cursor |
r | replace the current character |
R | replace the character after the cursor on the current line |
find command
hot key | operate |
---|---|
/ |
/xxxx, start the search from the cursor position, press n to search down, press N to search up |
? |
?xxxx, start the search from the cursor position, press n to search up, press N to search down |
# |
Move the cursor to the string to be searched, then press n to search up, but N to search down |
shift + k |
Press shift+k or K on the string to be searched to view the related help documentation |
1.3.2 Switch to text input mode
- To switch from command mode to text input mode simply enter the following command:
hot key | operate |
---|---|
i |
Insert before cursor |
a |
insert after cursor |
I |
Insert at the beginning of the line where the cursor is located |
A |
Insert at the end of the line where the cursor is located |
o |
Create a new line below the line where the cursor is, insert at the beginning of the line |
O |
Create a new line above the line where the cursor is, insert at the beginning of the line |
s |
Delete the character after the cursor, insert from the current position of the cursor |
S |
Delete the current line where the cursor is, insert from the beginning of the line |
Insert in column mode | Press first ctrl+v to enter column mode, press to hjkl move to select a column, press I or shift+i to insert forward, then insert character, and finally press twiceesc |
1.3.3 Operation in last line mode
- To switch from command mode to last line mode, enter a colon
(:)
save and exit
hot key | operate |
---|---|
q |
quit |
q! |
Force quit without saving changes |
w |
Save changes without exiting |
wq |
save and exit |
x |
equivalent to wq |
replace operation
The following table old
represents the original string, new
represents the new string
hot key | operate |
---|---|
:s/old/new/ |
The first line of the cursor is old replaced withnew |
:s/old/new/g |
old Replace everything on the line where the cursor is withnew |
:m, ns/old/new/g |
将第m 行至第n 行之间的old 全部替换成new |
:%s/old/new/g |
当前文件的所有old 替换为new |
:1,$s/old/new/g |
当前文件的所有old 替换为new |
:%s/old/new/gc |
同上,但是每次替换需要用户确认 |
快速翻屏
快捷键 | 操作 |
---|---|
ctrl + u |
向下翻半屏(up )–光标向上移动 |
ctrl + d |
向上翻半屏(down )–光标向下移动 |
ctrl + f |
向上翻一屏(front ) |
ctrl + b |
向后翻一屏(back ) |
- 在末行模式下执行shell命令
!shell
命令
按下两次esc
可以回到命令模式
分屏操作
- 在打开文件之后分屏:
快捷键 | 操作 |
---|---|
sp |
当前文件水平分屏 |
vsp |
当前文件垂直分屏 |
sp 文件名 |
当前文件和另一个文件水平分屏 |
vsp 文件名 |
当前文件和另一个文件垂直分屏 |
ctrl-w-w |
在多个窗口切换光标 |
wall/wqall/xall/qall/qall! |
保存/保存退出/保存退出/退出/强制退出分屏窗口 |
-
屏:
分屏:vim -on file1 file2 …
垂直分屏:vim -On file1 file2…
注意:n
可以省略, 有几个文件就分几屏 -
从末行模式切换回命令模式
按两次ESC
, 退格(backspace
)或者回车键
1.3.4 vim的配置文件
用户级别配置文件
~/.vimrc
, 修改用户级别的配置文件只会影响当前用户, 不会影响其他的用户.
例如: 在用户的家目录下的.vimrc
文件中添加
-
set tabstop=4
----设置缩进4个空格 -
set nu
----设置行号 -
set shiftwidth=4
—设置gg=G缩进4个空格, 默认是缩进8个空格
系统级别配置文件
/etc/vim/vimrc
, 修改了系统级别的配置文件将影响系统下的所有用户.
Note: 由于linux是多用户操作系统, 建议只在用户级别的配置文件下进行修改, 不要影响其他用户.
总结
I look forward to your communication with me, leave a message or private message, learn together and make progress together!