Early Unix used vi as the system default editor. What is the difference between vi and Vim? In fact, Vim is an upgraded version of vi. Many Linux system administrators are accustomed to using vi, because they use vi when they come into contact with Linux.
The biggest difference between vi and Vim is that when editing a text, vi will not display color, and Vim will display color. The display color is easier for users to edit, but the other functions are not much different
Vim has three modes: general mode, editing mode and command mode, which we need to keep in mind.
1. General mode
When we use the command vim [file name], we will enter the general mode of editing. In this mode, the operations we can do are: move the cursor up and down, delete a character, delete a line, and copy or paste one or more lines.
When we enter this command, we can enter the vim editing system, but we can't edit the file in this mode.
The ways to move the cursor in this mode are:
button | effect |
b or left arrow key | Cursor moves one position to the left |
Lowercase i or right arrow key | Cursor moves one position to the right |
k or up arrow | The cursor moves up one position |
j or down arrow | The cursor moves down one position |
Crtl+B | Text pages turn one page forward |
Crtl+F | Text page turns backward |
Number 0 or shift + 6 | Move to the beginning of the bank |
shift+4 | Move to the end of the line |
gg | Move to the beginning of the line |
G | Move to end of line |
nG (n is any number) | Move to line N |
In addition to the first time, in general mode, we can also copy, paste or delete characters or strings.
button | effect |
x and x | x means to delete one character backward, X means to delete one character forward |
nx | Delete n characters backward |
dd | Delete / cut the line where the cursor is |
ndd | Delete / cut all lines after the cursor |
yy | Copy the line where the cursor is |
p | Start from the line where the cursor is and paste the copied content down |
nyy | Start from the line where the cursor is, copy n lines down |
in | Restore the previous operation |
2. Edit mode
After entering the above command, we can't edit the file directly, we can only move the cursor. If we want to enter the edit mode, we also need to enter any command. This command only needs to press the key corresponding to the keyboard.
button | effect |
i | Insert before current character |
I |
Insert at the beginning of the line where the cursor is |
O | Insert a new line below the current line |
O | Insert a new line above the current line |
a | Insert after current character |
A | Insert at the end of the line where the cursor is |
When we press the i key, we can see the insert appear in the lower left of the page and enter the editing mode.
When we have finished entering the content, after pressing the esc key, we will exit the editing mode and enter the general mode.
3. Command mode
When we exit the editing mode, press / or: to enter the command mode, in this mode, we can search for a certain character or string, can also save, replace, exit, display line number and other operations.
- / word: Find a string word after the cursor, press n to continue searching
Enter / vigorously, you can search the vigorously in the edited content, as shown below.
-
? word: Find a string word before the cursor, press n to continue searching
-
: n1, n2s / word1 / word2 / g Find word1 between n1 and n2 lines 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, without g, only replace the first word1 of each line
Enter 1: $ s / 你 / you / g and replace all you in the document with you.
Command mode also has some other more important functions, such as saving files, exiting vim system, etc.
-
: w, save file
-
: q, quit VIM (note that if we modify the file, it must be saved before exiting, otherwise VIM will prompt, you have not saved, and it is not allowed to quit)
-
: w !, compulsory saving, under the root user, you can complete the saving even if the text is read-only
-
: q !, force quit, all changes will not take effect
-
: set nu display line number
-
: set nonu does not display line number
4. Comparison of vi and vim editing modes
The biggest difference between vi and Vim is that when editing a text, vi will not display color, and Vim will display color. The display color is easier for users to edit, but the other functions are not much different.
Use vim to edit the following code:
Edit the code using vi mode:
Haha, although there is no difference in other aspects, of course, as a cute little fairy, I still prefer to use vim, because it looks good ...