Differences between VI and VIM editors in Linux and their usage

Source: Internet
Author: User

From: http://blog.csdn.net/suofiya2008/article/details/5630356

VI and Vim are both editors. The difference is that Vim is more advanced and can be understood as an advanced version of VI. VI is like the event plan in windows, while Vim can be regarded as the word in office. VI is mainly used to edit some files. Vim is a good tool for programmers. Okay .... The following describes how to use vi ......
1. Use VI to enter the General Mode
[Root @ Linux ~] # Vi test.txt
Enter "VI file name" to enter VI. 10-2. The current status of the file is displayed in the lower left corner. If a new file is created, [new file] is displayed. If an existing file is created, the current file name, number of lines, and number of characters are displayed, for example, ""/etc/man. config "145l, 4614c ".

Figure 10-2 open a file using vi
2. Press I to enter the editing mode and start editing the text.
In normal mode, you only need to press the I, O, A and other characters to enter the editing mode. In edit mode, -- insert -- is displayed in the lower left corner, which means any characters can be entered, as shown in 10-3. At this time, except for the [ESC] button on the keyboard, other buttons can be regarded as general input buttons and can be edited (in VI, [Tab] the result of the key is different from that of the space character ).
Figure 10-3 enter the VI editing mode
3. Press the [ESC] key to return to normal mode.
Press the [ESC] key and you will immediately find the -- insert -- in the lower left corner of the screen disappears.
4. In general mode, press: WQ to store and Leave Vi
We want to save the file. The command for saving and leaving the disk is very simple. Enter ": WQ" to save the file and exit (Note: Press:, the cursor will move to the bottom line ). At this time, enter "ls -l133" at the end of the prompt to see the created test.txt file. The final result is 10-4.
Figure 10-4 uses the VI storage file. The test.txt file has been created. Note that if the File Permission is incorrect, for example,-r --, the file may not be written. Can I use the "Force write" method? Yes. Use ": WQ !" Add an exclamation point. However, note that this is feasible when "your permissions can be changed.
2 .... Command Line Content Description
As mentioned above, the command line or command mode does not display the words -- insert -- or -- replace -- in the bottom line. Generally, for commands in the command line, see Table 10-1 (note that when you press ":", the cursor will automatically move to the bottom line of the screen ).
Table 10-1 command line
General mode: move the cursor. h or move the cursor to the left)
Move a character to the left
J or downward arrow key)
Move the cursor down a character
K or the up arrow key)
Move the cursor one character up
General table continuation mode: The method for moving the cursor L or the right direction key (→)
Move the cursor one character to the right
If you want to move multiple times, for example, move 30 rows down, you can use the "30j" or "30 Gbit/s" combination key, that is, after adding the number (number) You want to perform the operation.
[CTRL] + [F]
The screen moves one page down, which is equivalent to a [Page Down] button (commonly used)
[CTRL] + [B]
The screen moves one page up, which is equivalent to a [page up] button (commonly used)
[CTRL] + [d]
Move half page down
[CTRL] + [u]
Screen "up" move half page
General mode: move the cursor +
Move the cursor to the next line without a space character
-
Move cursor to the previous line without space characters
N<Space>
NIndicates a number, for example, 20. Press the number and then press the Space key. The cursor will move the line to the right. NCharacters. For example, if the value is 20 <space>, the cursor moves the distance of 20 characters to the end.
0
This is the number "0": Move to the top character of this line (commonly used)
$
Move to the last character of this line (commonly used)
H
Move the cursor to the top line of the screen
M
Move the cursor to the center of the screen
L
Move the cursor to the bottom line of the screen
G
Move to the last line of the file (commonly used)
NG
NIs a number. Move to NLine. For example, 20 GB will be moved to the 20th rows of the file (can be used with: Set Nu)
Gg
Move to the first line of this file, equivalent to 1 GB (commonly used)
N<Enter>
NIs a number. Move the cursor downward NRow (common)
General mode: Search and replacement/Word
Start from the cursor position and look down for a string named word. For example, to search for the vbird string in the file, enter/vbird (commonly used)
? Word
Start from the cursor position and look up for a string named Word
N
N
Is an English button. Indicates "repeat the previous search action ". For example, if you just run/vbird to search for the vbird string, press NTo search for the Next string named vbird. If it is execution? Press vbird NTo search for a string named vbird.
N
This N is an English button. And NOn the contrary, perform the previous search operation for "reverse. For example, after/vbird, press n to search for vbird "up"

General table continuation mode: Search and replacement: N1, N2s/word1/word2/g
N1. N2 is a number. In N1. NSearch for the word1 string between two rows and replace it with word2. For example, if you search for vbird between 100 and 200 rows and replace it with vbird: ": 100, 200 s/vbird/g" (commonly used)
: 1, $ S/word1/word2/g
Search for the word1 string from the first row to the last row, and replace the string with word2 (commonly used)
: 1. $ S/word1/word2/GC
Search for the word1 string from the first row to the last row, and replace the string with word2. A prompt is displayed before replacement to check whether the conform needs to be replaced (commonly used)
Normal Mode: delete, copy, and paste X, X
In a line of text, X is to delete a character backward (equivalent to a [del] button), and X is to delete a character forward (equivalent to [backspace], that is, the Return key) (commonly used)
NX
NIs a number and is deleted backward. NCharacters. For example, to delete 10 characters in a row, enter "10x"
Dd
Delete the entire line of the cursor (commonly used)
NDd
NIs a number. From the cursor position, delete down NColumn. For example, 20 dd is used to delete 20 columns (commonly used)
D1g
Delete all data from the cursor position to the first row
DG
Delete all data from the cursor position to the last row
D $
Delete the last character from the cursor position to the row
D0
D is followed by the number 0, where the cursor is deleted, to the beginning of the line a character
YY
Copy the row where the cursor is located (frequently used)
NYY
NIs a number. Copy the cursor down NRows. For example, 20 YY is used to copy 20 rows (commonly used)
Y1g
Copy all data from the row where the cursor is located to the first row
YG
Copy all data from the row where the cursor is located to the last row
Y0
Copy the character of the cursor to all data at the beginning of the row.
Y $
Copy all data from the character with the cursor to the end of the row.
Normal Mode: delete, copy, and paste P, P
P: to paste the copied data to the next row of the cursor, P is to paste the row on the cursor. For example, the current cursor is in 20th rows and 10 rows of data have been copied. After you press P, the 10 rows of data will be pasted after the original 20 rows, that is, from 21 rows. However, if you press P, the original 20th rows will be changed to 30 rows (commonly used)
J
Combine the row where the cursor is located with the data in the next column into the same row
C
Multiple data records are deleted repeatedly, for example, 10 rows are deleted down and [10cj]
U
Restore the previous operation (commonly used)
[CTRL] + R
Redo the previous operation (commonly used)
U and [CTRL] + R are common commands. One is restoration, and the other is redo once. These two buttons are easy to edit.
General table resumption mode: delete, copy, and paste tables.
This is the decimal point. It means to repeat the previous action. If you want to delete or paste the file repeatedly, press the "." decimal point)
Enter edit mode I, I
Insert: insert the input text at the position of the current cursor. The existing text will be backward. Where, I is "insert from the position of the current cursor ", i: "Insert at the first non-space character in the current row" (commonly used)
A,
A is "insert from the next character of the current cursor", and a is "insert from the last character of the row where the cursor is located" (commonly used)
O, O
This is the case of the English letter O. O is "Insert a new row in the next row where the current cursor is located"; O is "Insert a new row in the previous row where the current cursor is located" (commonly used)
R, R
Replace: R will replace the character where the cursor is located; R will always Replace the text where the cursor is located until the ESC key is pressed (commonly used)
When using these buttons, the words "-- insert --" or "-- replace --" will appear in the lower left corner of the VI image. The operation is identified by name. Note: As mentioned above, when you want to input characters in the file, you must see insert/Replace in the lower left corner before entering the characters.
ESC
Exit edit mode and return to normal mode (commonly used)
Command Line command mode: W
Write edited data to Hard Disk Files (commonly used)
: W!
If the file attribute is "read-only", the file is forcibly written. But whether it can be written is related to the file permission.
: Q
Exit VI (commonly used)
: Q!
If you have modified the file and do not want to store it, use it! To forcibly exit files not stored
Note that the exclamation point (!) In VI, it is often "forced.
: WQ
After Storage, if it is: WQ! It is forced to store and then leave (commonly used)
: E!
Restore the file to the original state.
Zz
If the file is not changed, it is not stored and left. If the file has been changed, it is stored and left.
: W [filename]
Save edited data to another file (similar to saving new files)
: R [filename]
Read the data of another file in the edited data. Add the file "FILENAME" to the end of the row where the cursor is located.
: N1, N2 W [filename]
Set N1 N2 content is stored as a filename File
:! Command
Temporarily exit VI to display the result of Executing command in command line mode. For example:! Ls/home, you can view the LS output file information in/home in VI
: Set nu
Displays the row number. After the row is set, the row number is displayed in the prefix of each row.
: Set Nonu
Opposite to set Nu, the row number is canceled.
Note that in VI, "Numbers" are meaningful. A number usually indicates repeated times. It may also indicate where to go. For example, to delete 50 rows, use "50dd ". Add the number before the action. To move 20 rows down, use "20j" or "20 rows.
3... File restoration and temporary storage
VI has the "recoverable" function. What is VI for recovery? It's very simple. It's a temporary file. For example, if the name of a file is/tmp/passwd, a temporary file named "/tmp /. passwd. SWP ", this is a hidden file, some modifications we make will be temporarily stored in this file. If the system crashes during file modification, the system will tell you the next time you re-run VI/tmp/passwd, will the system tell you whether to restore (recovery) into the change process? If you press (R), you can recover the data to the modification process rather than the source file. This is a useful feature.
That is to say, if one day, when you execute LS-Al in/tmp, you will find that there are two files named passwd and. passwd. SWP, so (1) Someone may be editing this file; (2) When you were editing this file, the VI Program was interrupted due to unknown factors, the temporary file will exist. If it is status (2), you can set this. passwd. delete the SWP file, or directly VI/tmp/passwd. When VI asks whether the file is restored, Select Restore, store the update/tmp/passwd, and then store the/tmp /. passwd. delete the SWP file. If you do not delete the file, you will be notified every time you edit the file.

VI and Vim are both editors. The difference is that Vim is more advanced and can be understood as an advanced version of VI. VI is like the event plan in windows, while Vim can be regarded as the word in office. VI is mainly used to edit some files. Vim is a good tool for programmers. Okay .... The following describes how to use vi ......
1. Use VI to enter the General Mode
[Root @ Linux ~] # Vi test.txt
Enter "VI file name" to enter VI. 10-2. The current status of the file is displayed in the lower left corner. If a new file is created, [new file] is displayed. If an existing file is created, the current file name, number of lines, and number of characters are displayed, for example, ""/etc/man. config "145l, 4614c ".

Figure 10-2 open a file using vi
2. Press I to enter the editing mode and start editing the text.
In normal mode, you only need to press the I, O, A and other characters to enter the editing mode. In edit mode, -- insert -- is displayed in the lower left corner, which means any characters can be entered, as shown in 10-3. At this time, except for the [ESC] button on the keyboard, other buttons can be regarded as general input buttons and can be edited (in VI, [Tab] the result of the key is different from that of the space character ).
Figure 10-3 enter the VI editing mode
3. Press the [ESC] key to return to normal mode.
Press the [ESC] key and you will immediately find the -- insert -- in the lower left corner of the screen disappears.
4. In general mode, press: WQ to store and Leave Vi
We want to save the file. The command for saving and leaving the disk is very simple. Enter ": WQ" to save the file and exit (Note: Press:, the cursor will move to the bottom line ). At this time, enter "ls -l133" at the end of the prompt to see the created test.txt file. The final result is 10-4.
Figure 10-4 uses the VI storage file. The test.txt file has been created. Note that if the File Permission is incorrect, for example,-r --, the file may not be written. Can I use the "Force write" method? Yes. Use ": WQ !" Add an exclamation point. However, note that this is feasible when "your permissions can be changed.
2 .... Command Line Content Description
As mentioned above, the command line or command mode does not display the words -- insert -- or -- replace -- in the bottom line. Generally, for commands in the command line, see Table 10-1 (note that when you press ":", the cursor will automatically move to the bottom line of the screen ).
Table 10-1 command line
General mode: move the cursor. h or move the cursor to the left)
Move a character to the left
J or downward arrow key)
Move the cursor down a character
K or the up arrow key)
Move the cursor one character up
General table continuation mode: The method for moving the cursor L or the right direction key (→)
Move the cursor one character to the right
If you want to move multiple times, for example, move 30 rows down, you can use the "30j" or "30 Gbit/s" combination key, that is, after adding the number (number) You want to perform the operation.
[CTRL] + [F]
The screen moves one page down, which is equivalent to a [Page Down] button (commonly used)
[CTRL] + [B]
The screen moves one page up, which is equivalent to a [page up] button (commonly used)
[CTRL] + [d]
Move half page down
[CTRL] + [u]
Screen "up" move half page
General mode: move the cursor +
Move the cursor to the next line without a space character
-
Move cursor to the previous line without space characters
N<Space>
NIndicates a number, for example, 20. Press the number and then press the Space key. The cursor will move the line to the right. NCharacters. For example, if the value is 20 <space>, the cursor moves the distance of 20 characters to the end.
0
This is the number "0": Move to the top character of this line (commonly used)
$
Move to the last character of this line (commonly used)
H
Move the cursor to the top line of the screen
M
Move the cursor to the center of the screen
L
Move the cursor to the bottom line of the screen
G
Move to the last line of the file (commonly used)
NG
NIs a number. Move to NLine. For example, 20 GB will be moved to the 20th rows of the file (can be used with: Set Nu)
Gg
Move to the first line of this file, equivalent to 1 GB (commonly used)
N<Enter>
NIs a number. Move the cursor downward NRow (common)
General mode: Search and replacement/Word
Start from the cursor position and look down for a string named word. For example, to search for the vbird string in the file, enter/vbird (commonly used)
? Word
Start from the cursor position and look up for a string named Word
N
N
Is an English button. Indicates "repeat the previous search action ". For example, if you just run/vbird to search for the vbird string, press NTo search for the Next string named vbird. If it is execution? Press vbird NTo search for a string named vbird.
N
This N is an English button. And NOn the contrary, perform the previous search operation for "reverse. For example, after/vbird, press n to search for vbird "up"

General table continuation mode: Search and replacement: N1, N2s/word1/word2/g
N1. N2 is a number. In N1. NSearch for the word1 string between two rows and replace it with word2. For example, if you search for vbird between 100 and 200 rows and replace it with vbird: ": 100, 200 s/vbird/g" (commonly used)
: 1, $ S/word1/word2/g
Search for the word1 string from the first row to the last row, and replace the string with word2 (commonly used)
: 1. $ S/word1/word2/GC
Search for the word1 string from the first row to the last row, and replace the string with word2. A prompt is displayed before replacement to check whether the conform needs to be replaced (commonly used)
Normal Mode: delete, copy, and paste X, X
In a line of text, X is to delete a character backward (equivalent to a [del] button), and X is to delete a character forward (equivalent to [backspace], that is, the Return key) (commonly used)
NX
NIs a number and is deleted backward. NCharacters. For example, to delete 10 characters in a row, enter "10x"
Dd
Delete the entire line of the cursor (commonly used)
NDd
NIs a number. From the cursor position, delete down NColumn. For example, 20 dd is used to delete 20 columns (commonly used)
D1g
Delete all data from the cursor position to the first row
DG
Delete all data from the cursor position to the last row
D $
Delete the last character from the cursor position to the row
D0
D is followed by the number 0, where the cursor is deleted, to the beginning of the line a character
YY
Copy the row where the cursor is located (frequently used)
NYY
NIs a number. Copy the cursor down NRows. For example, 20 YY is used to copy 20 rows (commonly used)
Y1g
Copy all data from the row where the cursor is located to the first row
YG
Copy all data from the row where the cursor is located to the last row
Y0
Copy the character of the cursor to all data at the beginning of the row.
Y $
Copy all data from the character with the cursor to the end of the row.
Normal Mode: delete, copy, and paste P, P
P: to paste the copied data to the next row of the cursor, P is to paste the row on the cursor. For example, the current cursor is in 20th rows and 10 rows of data have been copied. After you press P, the 10 rows of data will be pasted after the original 20 rows, that is, from 21 rows. However, if you press P, the original 20th rows will be changed to 30 rows (commonly used)
J
Combine the row where the cursor is located with the data in the next column into the same row
C
Multiple data records are deleted repeatedly, for example, 10 rows are deleted down and [10cj]
U
Restore the previous operation (commonly used)
[CTRL] + R
Redo the previous operation (commonly used)
U and [CTRL] + R are common commands. One is restoration, and the other is redo once. These two buttons are easy to edit.
General table resumption mode: delete, copy, and paste tables.
This is the decimal point. It means to repeat the previous action. If you want to delete or paste the file repeatedly, press the "." decimal point)
Enter edit mode I, I
Insert: insert the input text at the position of the current cursor. The existing text will be backward. Where, I is "insert from the position of the current cursor ", i: "Insert at the first non-space character in the current row" (commonly used)
A,
A is "insert from the next character of the current cursor", and a is "insert from the last character of the row where the cursor is located" (commonly used)
O, O
This is the case of the English letter O. O is "Insert a new row in the next row where the current cursor is located"; O is "Insert a new row in the previous row where the current cursor is located" (commonly used)
R, R
Replace: R will replace the character where the cursor is located; R will always Replace the text where the cursor is located until the ESC key is pressed (commonly used)
When using these buttons, the words "-- insert --" or "-- replace --" will appear in the lower left corner of the VI image. The operation is identified by name. Note: As mentioned above, when you want to input characters in the file, you must see insert/Replace in the lower left corner before entering the characters.
ESC
Exit edit mode and return to normal mode (commonly used)
Command Line command mode: W
Write edited data to Hard Disk Files (commonly used)
: W!
If the file attribute is "read-only", the file is forcibly written. But whether it can be written is related to the file permission.
: Q
Exit VI (commonly used)
: Q!
If you have modified the file and do not want to store it, use it! To forcibly exit files not stored
Note that the exclamation point (!) In VI, it is often "forced.
: WQ
After Storage, if it is: WQ! It is forced to store and then leave (commonly used)
: E!
Restore the file to the original state.
Zz
If the file is not changed, it is not stored and left. If the file has been changed, it is stored and left.
: W [filename]
Save edited data to another file (similar to saving new files)
: R [filename]
Read the data of another file in the edited data. Add the file "FILENAME" to the end of the row where the cursor is located.
: N1, N2 W [filename]
Set N1 N2 content is stored as a filename File
:! Command
Temporarily exit VI to display the result of Executing command in command line mode. For example:! Ls/home, you can view the LS output file information in/home in VI
: Set nu
Displays the row number. After the row is set, the row number is displayed in the prefix of each row.
: Set Nonu
Opposite to set Nu, the row number is canceled.
Note that in VI, "Numbers" are meaningful. A number usually indicates repeated times. It may also indicate where to go. For example, to delete 50 rows, use "50dd ". Add the number before the action. To move 20 rows down, use "20j" or "20 rows.
3... File restoration and temporary storage
VI has the "recoverable" function. What is VI for recovery? It's very simple. It's a temporary file. For example, if the name of a file is/tmp/passwd, a temporary file named "/tmp /. passwd. SWP ", this is a hidden file, some modifications we make will be temporarily stored in this file. If the system crashes during file modification, the system will tell you the next time you re-run VI/tmp/passwd, will the system tell you whether to restore (recovery) into the change process? If you press (R), you can recover the data to the modification process rather than the source file. This is a useful feature.
That is to say, if one day, when you execute LS-Al in/tmp, you will find that there are two files named passwd and. passwd. SWP, so (1) Someone may be editing this file; (2) When you were editing this file, the VI Program was interrupted due to unknown factors, the temporary file will exist. If it is status (2), you can set this. passwd. delete the SWP file, or directly VI/tmp/passwd. When VI asks whether the file is restored, Select Restore, store the update/tmp/passwd, and then store the/tmp /. passwd. delete the SWP file. If you do not delete the file, you will be notified every time you edit the file.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.