Text editing tool vim
Chapter 9 Text editing toolVim
Vim is an essential tool in linux. A lot of work cannot be done without it. Early Unix systems used vi as the system's default editor. You may be wondering what is the difference between vi and vim? Vim is an upgraded version of vi. Many linux system administrators are used to vi, because vi was used when they came into contact with linux, and vim became popular later. Therefore, it doesn't matter if you use vi and vim, as long as you can achieve what you want.
In my opinion, the biggest difference between vi and vim is that when you edit a text, vi will not display the color, while vim will display the color. The display color is easier for users to edit. There is no difference between other functions. In linux, using vi or vim depends entirely on your personal interests. I have been using vim since I learned linux, so I will always teach you the vim role.
Vim has three modes: general mode, edit mode, and command mode. This should be noted in mind, because I used to go to the interview when I was just engaged in linux work, and many unit pen questions have this knowledge point.
* Normal Mode: When vim filename is used to edit a file, the normal mode is used as soon as the file is entered. In this mode, you can move the cursor up or down, delete a character, delete a row, and copy or paste one or more rows.
* Editing mode: In normal mode, a character cannot be modified, but only in editing mode. Enter the editing mode in normal mode. You only need to press a key (I, I, A, a, o, O, r, R ). When you enter the editing mode, the INSERT or REPLACE line appears at the bottom of the screen. Back from the editing mode to the general mode, you only need to press the ESC key in the upper-left corner of the keyboard.
* Command mode: In normal mode, enter ":" or "/" to enter the command mode. In this mode, you can search for a character or string, save, replace, exit, and display the row number.
Below I will teach you how to write a piece of text in a blank document and save it.
Enter vim test.txt and press enter to enter the normal mode. Press the "I" Letter to enter the editing mode.
"INSERT" appears at the bottom left of the window, indicating that the editing mode is in progress and the content can be written.
After the content is edited, Press ESC to exit the editing mode and enter the normal mode. At this point, the "INSERT" in the lower left disappears, and then press ":" to enter the command mode, and finally enter wq to save and exit vim.
In this case, refer to the content of the test.txt document.
In fact, vim is a keyboard editor, so there are many function keyboards in each mode. The following is an example of what I think is often marked in red. You need to exercise more, and you also need to know what is not commonly used.
Move the cursor in Normal Mode |
H or left direction key |
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 |
L or the right arrow key |
Move the cursor one character to the right |
Ctrl + f or pageUP |
Move one page forward |
Ctrl + B or pageDOWN |
Move one page behind the screen |
Ctrl + d |
Move half of the screen forward |
Ctrl + u |
Move half of the screen back |
+ |
Move the cursor to the next column with a non-space character |
- |
Move the cursor to the previous column of a non-space character |
N spaces (n is a number) |
Press the number n and press the space. The cursor moves n characters to the right. If the number of characters in the row is less than n, the cursor continues to move from the downlink to the right until n |
0 (number 0) or Shift + 6 |
Move to the beginning of the row |
Shift + 4 |
That is, '$' is moved to the end of the row. |
H |
Move the cursor to the top line of the current screen |
M |
Move the cursor to the center of the current screen |
L |
Move the cursor to tHe bottommost line of the current screen |
G |
Move cursor to the last line of text |
NG (n is a number) |
Move to the nth line of the text |
Gg |
Move the first line with the text |
N press enter (n is a number) |
Move the cursor down n rows |
Search and replace in Normal Mode |
/Word |
Search for a string named word after the cursor. When the first word is found, press "n" to continue searching for the next one. |
? Word |
Search for a string named word before the cursor. When the first word is found, press "n" to continue searching for the previous one. |
: N1, n2s/word1/word2/g |
Search for the word1 string between n1 and n2 and replace it with word2. You can also Replace "/" "#" |
: 1, $ s/word1/word2/g |
From the first row to the last row, search for word1 and replace it with word2. |
: 1, $ s/word1/word2/gc |
The role of c is to be confirmed by the user before replacement. |
Delete in Normal Mode,Copy and paste |
X, X |
X deletes one character backward, and X deletes one character forward. |
Nx (n is a number) |
Delete n characters from the backend |
Dd |
Delete the row where the cursor is located |
Ndd (n is a number) |
Delete the next n rows of the cursor |
D1G |
Delete all data from the row where the cursor is located to the first row |
DG |
Delete all data from the row where the cursor is located to the last row |
Yy |
Copy the row where the cursor is located |
Nyy |
Copy n rows down from the row where the cursor is located |
P, P |
The data copied by p is pasted from the next row of the cursor, and P is pasted from the previous row of the cursor. |
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 |
J |
Combines the row where the cursor is located with the data of the next row into the same row. |
U |
Restore previous operations |
I |
Insert characters before the current character |
I |
Insert characters at the beginning of the current row |
A |
Insert characters after the current character |
A |
Insert characters at the end of the current row |
O |
Insert a new row under the current row |
O |
Insert a new row into the current row |
R |
Replace the character of the cursor only once |
R |
Always Replace the character of the cursor until you Press ESC |
: W |
Save edited text |
: W! |
If the text attribute is read-only, force save |
: Q |
Exit vim |
: Q! |
Do not save or exit after editing |
: Wq |
Save and exit |
: E! |
Restore a document to its original state |
ZZ |
If the document is not changed, it is not stored and left. If the document has changed, it is stored and left, equivalent to: wq |
: W [filename] |
Save the edited file as filename |
: R [filename] |
Read the content of the filename document under the row where the current cursor is located |
: Set nu |
Display the row number at the beginning of each row |
: Set nonu |
Cancel row number |
N1, n2 w [filename] |
Save the content from n1 to n2 as filename. |
:! Command |
Temporarily leave vim to run a linux Command, for example :! Ls/home: List Files in the/home directory temporarily, and then press enter to return to vim |
So much for the time being. If you have all the skills, you are the vim master. If you think too much, you just need to remember the red part of the author. If you want to use other products, you can check them again. The following is a small assignment for you. I hope you will finish it carefully!
1. Copy/etc/init. d/iptables to the/root/directory and rename it test.txt.
2. Use vimto open test.txt and set the row number.
3. Move 5 characters down, right, left, and right respectively
4. Flip down and up two pages respectively
5. move the cursor to 49th rows.
6. move the cursor to the end of the row and then to the beginning of the row.
7. Move to the last line of the test.txt File
8. Move to the first line of the file
9. Search for the iptables in the file and count the total number of iptables.
10. Replace iptables from the first row to the third row with iptable
11. Restore the previous operation
12. replace all iptables in the entire file with iptable
13. move the cursor to 50 rows and delete the character "$"
14. Restore the previous operation
15. Delete row 50th
16. Restore the previous operation
17. Delete all contents from row 37 to row 42
18. Restore the previous operation
19. Copy 48 rows and paste them under 52 rows.
20. Restore the previous operation (two times u)
21. copy and paste the content from line 37 to line 42 to line 44.
23. Restore the previous operation (two times u)
24. Move the content from 37 rows to 42 rows under 19 rows
25. Restore the previous operation (two times u)
26. move the cursor to the first line and change/bin/sh to/bin/bash.
27. Insert a new row under the first line and enter "# Hello !"
28. Save the document and exit