vi and vim (fine creation)

vi & vim

1. Background introduction and the difference between vi and vim

      vi is the default editor of linux system, maybe you will ask? What is the difference between vi and vim? In fact, vim is an upgraded version of vi. In my opinion, the biggest difference between vi and vim is that when editing a total of text, vi will not display colors, but vim will display colors.

2. If your system does not have vim, execute the following statement

yum install -y vim-enhanced

3. Brief introduction to vim

      There are three modes of vim: general mode, editing mode, and command mode, which we need to keep in mind.

3.1 General mode

      When we use the command vim filename to edit a file, it enters the general mode of the file by default. In this mode, you can do operations: move the cursor up and down, delete a character, delete a line, and copy and paste one or more lines.

button Use
h or the left arrow key Move the cursor one character to the left
l (lowercase letter) or right arrow key Move the cursor one character to the right
k or up arrow key Cursor up one character
j or the left arrow key Cursor down one character
Ctrl + B Turn the text page forward one page
Ctrl + F Turn the text page back one page
Number 0 or shift + 6 Move to the beginning of this line
shift + 4 Move to the end of the line
gg Move to the first line
G Move to the end
nG (n is any number) Move to line n

      In the general mode, we can also implement operations such as copying, deleting, and pasting characters or strings, as shown in the following table

button effect
x or x x means delete one character backward, X means delete one character forward
nx Delete n characters backward
dd Delete/cut the line where the cursor is
ndd (n is the abbreviation of number) Delete/cut n lines after the line where the cursor is
yy Copy the line where the cursor is
nyy From the line where the cursor is located, copy n lines down
p Starting from the line where the cursor is located, paste down the copied or pasted content
P Starting from the line where the cursor is, paste the copied or pasted content upwards
u Restore the previous operation
v After pressing v, moving the cursor will select the specified character, and then copy, paste and other operations can be realized

3.2 Edit mode

      You cannot modify a character in the general mode. If you want to modify a character, you can only enter the edit mode. To enter edit mode from normal mode , just press one of i, I, a, A, o, O, r, or R. When entering the edit mode, the word INSERT or REPLACE will be displayed at the end of the screen (if your centos supports Chinese, it will display "Insert"). **To return to normal mode from edit mode, just press Esc key. **As shown below

button Use
i Insert before the current character
I Insert at the beginning of the line where the cursor is
a Insert after the current character
A Insert at the end of the line where the cursor is
The Insert a new line below the current line
THE Insert a line above the current line

3.3 Command mode

      In the normal mode, enter: or / to enter the command mode. In this mode, we can search for a character or string, and can also perform operations such as saving, replacing, exiting, and displaying line numbers, as shown in the following table

button Use
/word Find a string word after the cursor, press n to continue searching backward
?word Find a string word before the cursor, press n to continue searching forward
:n1,n2s/word1/word2/g Find word1 between lines n1 and n2 and replace it with word2, without g, only replace the first word1 of each line
:1,$s/word1/word2/g Replace all word1 in the document with word2, and replace only the first word1 in each line without g

      Other functions are as follows

button Use
:w Save text
:q Exit vim
:w! Forced to save, under the root user, even if the text is read-only, it can be saved
:q! Force exit, all changes will not take effect
:wq Save and exit
: set no Show line number
:set nonu Do not display line numbers

Thanks for reading, please subscribe if you like.

Guess you like

Origin blog.csdn.net/qq_44112474/article/details/103403685