environment:
- centos7.6
- ubuntu 22.04.1
1. History of vi and vim
Reference: "Why Vim uses HJKL keys as arrow keys"
In 1979, Bill Joy created vi (also known as: ed>em>en>ex before this). Since the program was written ADM-3A 终端机
on , the terminal is as shown in the figure below:
Its keyboard is as follows:
So, naturally, use HJKL
the representation 左下上右
. . .
However, the function of vi is too simple, so Bram Moolenaar developed it in 1991 vim
and enhanced it, such as: supporting the up, down, left, and right arrows of the current keyboard, syntax highlighting, etc.
Later, there were some other variants, such as: NeoVim
, BusyBox
etc., but here we only care vi/vim
.
2. Install vim
2.1 centos7.6
It is generally believed that vim is not pre-installed (does not support vim
commands), but there is vi
, we can yum list installed | grep vi
check as follows:
It seems to be installed
vim
, but the function is not as good as the normal installationvim
, so let's think that centos7.6 is not pre-installedvim
.
Normal installation command: yum install vim -y
After the installation is complete, check again:
At this time, use vim to edit as follows ( vim test.c
):
Note, to use
vim
instead ofvi
2.2 free22.04.1
There is no pre-installed vim (does not support vim
commands), but there is vi
, check as follows:
Use the command to install, sudo apt install vim -y
, check after installation:
At this point, use vim to edit as follows ( vim test.c
):
3. Grammar
3.1 Three working modes of vim
Let's try this pattern:
Note:
vim test.c
Open it directly when test.c exists, open a blank one when it does not exist, and save it to test.c when it is saved, which is the same as the window notepad.
First, enter vim demo.c
(not in the current directory demo.c
, note that [New File] is also displayed at the bottom):
At this point we're in 普通模式
.
Let's not do other operations, just enter in the English input state i
, and then enter 编辑模式
, as follows:
In the above picture, we i
entered after we entered 编辑模式
, and we also entered a line of code.
Now, let's save and exit: first press ESC
enter 普通模式
, then enter ":"
enter 命令模式
, then enter wq
and exit.
The effect of the whole operation is as follows:
After understanding these three modes, the following is easy to say.
3.2 Common operations
3.2.1 Moving the Cursor
-
Under
普通模式
and编辑模式
below:
- use上下左右
the arrows for simple movement;
- useHome
the andEnd
keys to move to the beginning and end of the line;
- usePageUp
the andPageDown
keys to turn pages up and down; -
Below
普通模式
:
-g
positioning to the beginning of the entire text;
-G
positioning to the end of the entire text;
-H
positioning to the beginning of the screen;
-L
positioning to the end of the screen;
-M
positioning to the middle of the screen;
3.2.2 Search
In 命令模式
or 普通模式
under: Enter /查找的内容
Enter to search downward, Enter ?查找的内容
Enter to search upward, press during search n
to locate the next hit, continue to enter :noh
to clear the search result mark.
The following is only for demonstration /查找的内容
:
3.2.3 Copy, Paste, Delete, Undo
Below一般模式
, we:
Input to yy
copy the line where the cursor is located
Input p
to paste the copy just now Input to
copy the content of the 4 lines under the current cursor4yy
Enter dd
to delete the current line
Enter D
to delete the content from the current position of the cursor to the end of the line
Enter u
to undo the previous action
3.2.4 Shortcut operation
In 一般模式
, we:
Enter o
, insert a line under the current cursor, and enter 编辑模式
Enter A
, position the cursor to the end of the line, and enter 编辑模式
Enter zz
, quickly save and exit
3.2.5 Visual blocks
In fact, this kind of operation is window
like notepad++
the block operation in the following, as follows:
Then let's see vim
how it works?
First, we put the cursor in a good position, and then 普通模式
press to ctrl+v
enter the visual block operation:
then, we press 上下左右
the arrow to adjust the visual block boundary;
when we need to delete, d
just input directly.
When we need to insert, press uppercase I
to enter editing, after editing, press twice ESC
to exit to see the effect.
Let’s first look at the operation of a block deletion:
then look at the operation of a block insertion:
4. Principle of swp file
When we vim demo.c
open a file with , a file (in binary format, not plain text) is vim
automatically generated in the same directory . This is to prevent data loss due to abnormal exit of the program, or multiple people editing a file at the same time..demo.c.swp
When vim
normal editing exits, demo.c.swp
the file disappears.
Now, simulate a situation: 编辑一个文件,中间shell窗口突然关闭
, how to restore the data at this time?
First, prepare an existing file, as follows:
Then, we use vim test.txt
Open to edit halfway:
At this point, we change another shell to view the contents of this directory:
At this point, we directly close the previous window, and then look at the contents of the directory:
nothing has changed. . .
At this point, we use the current shell window to open this text: vi test.txt
, and the prompt is as follows: This is a prompt, because the file
already exists , and we are not allowed to open it directly..test.txt.swp
If we want to re-edit, that is, delete it .test.txt.swp
and open it normally, then we enter it here D
, so that the swp file will be deleted and entered vim
(note: the data has been lost).
If we want to restore the data we edited last time, that is, .test.txt.swp
from it test.txt
, then we enter here R
and then Enter
the key, so that the data is restored and we proceed vim
.