Linux: the use of vi and vim

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:

insert image description here
Its keyboard is as follows:
insert image description here
So, naturally, use HJKLthe representation 左下上右. . .

However, the function of vi is too simple, so Bram Moolenaar developed it in 1991 vimand 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, BusyBoxetc., 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 vimcommands), but there is vi, we can yum list installed | grep vicheck as follows:
insert image description here

It seems to be installed vim, but the function is not as good as the normal installation vim, so let's think that centos7.6 is not pre-installed vim.

Normal installation command: yum install vim -y
After the installation is complete, check again:
insert image description here
At this time, use vim to edit as follows ( vim test.c):
insert image description here

Note, to use viminstead ofvi

2.2 free22.04.1

There is no pre-installed vim (does not support vimcommands), but there is vi, check as follows:

insert image description here
Use the command to install, sudo apt install vim -y, check after installation:
insert image description here
At this point, use vim to edit as follows ( vim test.c):
insert image description here

3. Grammar

3.1 Three working modes of vim

insert image description here

Let's try this pattern:

Note: vim test.cOpen 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):
insert image description here

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:
insert image description here
In the above picture, we ientered after we entered 编辑模式, and we also entered a line of code.

Now, let's save and exit: first press ESCenter 普通模式, then enter ":"enter 命令模式, then enter wqand exit.
The effect of the whole operation is as follows:
insert image description here
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;
    - use Homethe and Endkeys to move to the beginning and end of the line;
    - use PageUpthe and PageDownkeys to turn pages up and down;

  • Below普通模式 :
    - gpositioning to the beginning of the entire text;
    - Gpositioning to the end of the entire text;
    - Hpositioning to the beginning of the screen;
    - Lpositioning to the end of the screen;
    - Mpositioning 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 nto locate the next hit, continue to enter :nohto clear the search result mark.

The following is only for demonstration /查找的内容:
insert image description here

3.2.3 Copy, Paste, Delete, Undo

Below一般模式 , we:

Input to yycopy the line where the cursor is located
Input pto paste the copy just now Input to
copy the content of the 4 lines under the current cursor4yy

Enter ddto delete the current line
Enter Dto delete the content from the current position of the cursor to the end of the line

Enter uto 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 windowlike notepad++the block operation in the following, as follows:
insert image description here

Then let's see vimhow it works?
First, we put the cursor in a good position, and then 普通模式press to ctrl+venter the visual block operation:
then, we press 上下左右the arrow to adjust the visual block boundary;
when we need to delete, djust input directly.
When we need to insert, press uppercase Ito enter editing, after editing, press twice ESCto exit to see the effect.

Let’s first look at the operation of a block deletion:
insert image description here
then look at the operation of a block insertion:
insert image description here

4. Principle of swp file

When we vim demo.copen a file with , a file (in binary format, not plain text) is vimautomatically 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 vimnormal editing exits, demo.c.swpthe file disappears.

Now, simulate a situation: 编辑一个文件,中间shell窗口突然关闭, how to restore the data at this time?

First, prepare an existing file, as follows:

insert image description here

Then, we use vim test.txtOpen to edit halfway:
insert image description here
At this point, we change another shell to view the contents of this directory:
insert image description here

At this point, we directly close the previous window, and then look at the contents of the directory:
insert image description here
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
insert image description here
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.swpand 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.swpfrom it test.txt, then we enter here Rand then Enterthe key, so that the data is restored and we proceed vim.

Guess you like

Origin blog.csdn.net/u010476739/article/details/127404802