Table of contents
foreword
This article is based on the notes and insights summarized after watching the dark horse programmer video at station B.
Introduction to vi / vim editor
vi is the abbreviation of visual interface. It is a text editing tool provided by the Linux system, which can edit the file content, similar to Notepad in Windows.
vim is an enhanced version of vi. It is compatible with all commands of vi. It can not only edit text, but also has the function of editing shell programs. It can distinguish the correctness of grammar with fonts of different colors, which greatly facilitates the design and editing of programs. .
Operating mode
Three modes of vi editor
- command mode
- input mode
- Bottom line mode (also called bottom line command mode)
Command mode: When using vi editor, it is in command mode by default. In this mode, the keystrokes are interpreted as commands by the editor, and the commands are used to drive different functions. In this mode, text editing is not possible freely.
Input mode: also known as edit mode and insert mode. Enter lowercase letter i or a or o in command mode to enter this mode. In this mode, the file content can be freely edited.
Bottom line mode: Enter the English colon: in the command mode to enter the bottom line mode, which is usually used for saving and exiting files.
The relationship between vi three modes
It can be seen from the figure that the command mode is equivalent to a transfer station, that is, no matter what mode we are currently in, we can enter the command mode by pressing the Esc key, and then continue to complete subsequent operations as required.
Four modes of vim
In addition to the three modes of vi, vim also has avisualization mode
That is, the four modes of vim are:
- command mode
- insert mode
- Bottom Row Mode
- visualization mode
Some materials also refer to the four modes of vim as: normal mode, insert mode, command mode, and visual mode
Here, we can simply think that vim has one more visual mode than vi
Visualization mode: Enter v
or orV
in the command mode to enter the visualization mode. In the visualization mode, you can select an editing area, and then perform operations such as inserting, deleting, and replacing the selected file contentCtrl + v
Character selection mode: select all characters passed by the cursor, press lowercase letter v to enter in command mode Line
selection mode: select all lines passed by the cursor, press capital letter V in command mode to enter
block selection mode: select all contents of a rectangular block, Press Ctrl + v in command mode to enter
Common operations in visualization mode
d
Delete the selected text
c
Modify the selected text, that is, delete the selected text, and then enter the desired content
r
Replace the selected text, replace the selected text with a single character (for example, r
press and then press 6, all the selected content will be replaced with 6)
gu
Selected area Convert to lowercase
gU
Selected area is uppercase
g~
Swap uppercase and lowercase
Initial use of vi/vim editor
vi / vim editor to edit files
vi file path
vim file path
vim is compatible with all vi functions, and all vim commands can be used later
- If the file path represents a file
不存在
, then this command will be used for编辑新文件
- If the file path represents a file
存在
, then this command is used to编辑已有文件
Some Linux operating systems do not have the vim editor installed by default (the system may have a vi editor). When we enter [vim file path], the system prompts that the command cannot be found, and the vim editor needs to be installed.
Install vim editor via yum tool:
yum install vim -y
Quick experience
- Use: vim hello.txt, edit a new file, enter the command mode after execution
- In the command mode, press the keyboard i to enter the input mode
- Enter in the input mode: hello vim
- After typing, press Esc to return to command mode
- In the command mode, press the English colon on the keyboard: to enter the bottom line mode
- Enter wq in the bottom line mode, save the file and exit the vim editor
command mode shortcut
Just now we mentioned that you can enter the input mode from the command mode through the [iao] key on the keyboard, so what is the difference between these commands?
Answer: For these commands, when entering the input mode, the position of the mouse cursor is different.
In addition to the above command shortcut keys, there are many shortcut keys in the command mode
save and exit
:w
=> Save (just save the current file, but not exit the file)
:q
=> Exit, note that the file must be saved before exiting
:wq
=> Save and exit
:q!
=> Force exit (the file is not saved at this time)
Under normal circumstances, after we edit the file, use :wq
the save and exit file
Abnormal exit solution
Abnormal exit: refers to exiting the file by directly closing the terminal or forcing exit after editing the file without saving and exiting normally.
At this time, the effect of the following figure will be displayed:
This abnormal exit will .文件名.swp
generate a swap file
for which we only needdelete the swap fileYou can edit the file normally (press the D key in the above prompt interface or specify to delete the swap file through rm)
rm .hello.txt.swp
Right nowrm 交换文件名称
This concludes this article! If there are mistakes, please correct me!