First, the editing mode
Command-line mode
When replaced
If there is a/symbol in the character you want to replace, you can change the delimiter to # or @
:1,100s#/good/weer/#well#g
Replace the/good/weer/in the first 100 rows with the well
,
: Nohl highlighting when you cancel a search
,
: X is also saved and exited, but if the file contents are not changed, Wq will modify the Mtime
X does not modify Mtime
Vim Exercises
Cp/etc/dnsmasq.conf/tmp/1.txt
(1) Move Down, right, left, and up 6 characters (6j 6l 6h 6k) respectively.
(2) Two pages (two times ctrl+f and Ctrl+b respectively) downward and upward respectively.
(3) Move the cursor to line 49th (49G).
(4) Move the cursor to the end of the line, and then move to the beginning (Shift+4, shift+6).
(5) Move to the last line of the 1.txt file (G).
(6) Move to the first line of the file (GG).
(7) Search for "DNSMASQ" in the file and count the number of occurrences of the string (/dnsmsq then press N).
(8) Replace the DNSMASQ from line 1th to line 10th with DNS (: 1,10s/dnsmasq/dns/g).
(9) Restore the previous action (U).
(10) Replace all the etc in the entire file with a CTE (: 1, $s/etc/cte/g).
(11) Move the cursor to line 25th, delete the string "ly" (25G then press L to move the cursor to the right to find "ly", press V to select, and press X).
(12) Restore the previous action (U).
(13) Delete line 50th (50G dd).
(14) Restore the previous action (U).
(15) Delete all contents of 第37-42 line (37G 6DD).
(16) Restore the previous action (U).
(17) Copy the contents of line 48th and paste it below line 52nd (48G yy 52G p).
(18) Restore the previous action (U).
(19) Copy the contents of the 第37-42 line and paste it onto line 44th (37G 6yy 44G P).
(20) Restore the previous action (press U).
(21) Move the contents of the 第37-42 line below line 19th (37G 6DD 19G p).
(22) Restore the previous action (press U).
(23) Move the cursor to the first line, the 1th line to the "#!/bin/bash" (First press GG, the cursor to the 1th line, and then press the letter A, enter the editing mode, and > Cursor to the end of the line, to modify the operation, after completion, press ESC).
(24) Insert a new line below line 1th and enter "# hello!" (Press O to enter edit mode, while the cursor goes down another line, enter "# hello!" )。
(25) Save the document and exit (press the ESC key and enter ": Wq").
Ii. Vim Usage Supplement
1.vim Editor replacement mode and visual mode
In general mode, press R and R on the keyboard to enter replacement mode. If you press small r then you enter the replacement mode, and the next character you enter will replace the character where you currently have the cursor, and then automatically exit the replacement mode. If you press the big R then all the characters you enter below will replace the following characters in turn until you press exit to replace mode.
Press V and V on the keyboard to enter the visual mode in normal mode. If you press small v then it is time to enter the view mode, when you move the cursor will be at the cursor at the end of all the characters selected, you can copy, delete and so on. If the large V is also entered in the view mode, this time moving the cursor will be the line to the end of the cursor line of all the contents of the selection, you can also copy, delete and so on.
Note: Use the "Ctrl + V" key combination in general mode to enter block operation mode
In this mode and the visual mode is similar, but the choice of content is different, you can actually see
2. Remove the character from the cursor to the end of the line
Enter uppercase "D" in general mode or enter "d$"
3. Remove the character from the cursor to the beginning of the line
Enter uppercase "d^" in general mode
4. Delete the line from the cursor line to the end of the file
Enter the uppercase "DG" in general mode
5. Delete all rows in the specified range
Example: Delete all contents from 10 to 15 rows
Enter ": 10,15d" in general mode
6. Save the file you are editing as a new file
Example: Save the file being edited as "/root/" and save it as 1.txt
Enter ": W/root/1.txt" in general mode
Example: Save line 10th to line 15th of the file being edited as 1.txt and save to root directory Enter ": 10,15 w/root/1.txt" in general mode
7. Import the contents of other files into the file where you are editing the cursor
Example: Import the contents of the "/root/1.txt" file into the next 10th line of the edited file
First, in general mode, press "10G" to position the cursor to line 10th.
Then press "O" in lowercase, another line below the current line, and enter insert mode
Finally press RETURN to general mode on the keyboard, and then enter ": R/root/1.txt" carriage return
8. When you are editing a file, you can still run the Linux command without exiting the file
Column: I'm editing a file, but at this point I want to see the contents of the "/root/1.txt" file, but I don't want to quit the file I'm editing, so we can
In edit mode, enter ":! Cat/root/1.txt "
9. Import the execution results of the command into the file where you are editing the cursor
This problem we can combine the above two questions, in the general mode of input ": R!" Cat/root/1.txt "
10. Find the replacement function using
Example: Add "#" at the beginning of lines 10 to 15
Enter ": 10,15s/^/#/" in general mode
Example: Remove "#" at the beginning of line 10 to 15
Enter ": 10,15s/^#//" in general mode
Example: Add "//" at the beginning of line 10 to 15
Enter ": 10,15s/^/\/\//" or ": 10,[email protected]^@//@" or ": 10,15s#^#//#" in general mode
Note: On the last side of all the commands can be used together with G or C, g means that all occurrences of the specified word nonalphanumeric substitution, but if the addition of G so that the location of the definition of the character, and vice versa before the position of the definition of characters, then you can not appear after G. Add C in the following can interact with the user, in the search to find the characters matching the command prompt the user whether to replace, require user confirmation, or do not need to confirm
11. Replace the specified character with the specified character
Example: When editing a document, I would like to enter a continuous string such as "abcdefghijklmnopqrstuvwxyz", when I want to enter only one or a string of specified characters can be replaced with just the character, such as I specify the input "aming" system will automatically put " Aming "Replace with" ABCDEFGHIJKLMNOPQRSTUVWXYZ "
Enter ": AB aming abcdefghijklmnopqrstuvwxyz" in the general mode and then enter the edit mode, and when you enter "aming" you will find that the "ABCDEFGHIJKLMNOPQRSTUVWXYZ" is automatically replaced.
12. Definition of shortcut keys
Example: I want to press the Ctrl+b shortcut key on the keyboard in normal mode, and automatically insert the "#" sign at the beginning of the line where the cursor is located, and then automatically exit to normal mode
In general mode, enter ": Map Ctrl + V ctrl+b I #" and then enter, this time in the general mode by pressing the ctrl+b on the keyboard will be at the beginning of the cursor inserted "#" number
Note: the command Ctrl + V and CTRL+B are key combinations on the keyboard, not the characters entered, it is necessary to press the key combination, where the first CTRL + V is pressed, the second ctrl+b is to define the shortcut key, according to the settings you need to press. Then "I" means "I" in the normal mode to enter the insertion mode and move the cursor to the beginning of the line, and then enter the "#" sign, after "" means to exit the edit mode
13. Edit two files in the same file or
Example: I am now editing the 1.txt file, and then I want to open the root directory of the 2.txt simultaneous editing, and the window up and down horizontally separated, together display
Enter ": New/root/2.txt" in general mode
Example: I am now editing the 1.txt file, then I want to open the root directory of 2.txt simultaneous editing, and the window left and right vertically separated, together display
Enter ": Vsplit/root/2.txt" in general mode
Note: In general mode Press "CTRL+W" key combination, and then press the left or right, or up and down the direction of the key, you can switch between different windows if you enter ": only" in the general mode, only keep the currently editing window, all other closed
15. Case insensitive when vim looks up keywords
Enter ": Set IC" in general mode
If you want to cancel, enter ": Set Noic"
16. How to set the file as a read-only file and save it only when forced to save
Enter ": Set readonly" in general mode
17. Restore the file to its open state
Enter ": e!" in general mode
18. Use of configuration files
Above so many operations, such as setting ignore case, set shortcut keys, set automatic replacement, and so on some operations, when the computer restarts, there is no. At this point we can write these commands to the configuration file, so that after the restart of the computer can still be used, we have two ways
The first type: All users are unified
Modify the "/ETC/VIMRC" file, add the command you want to set at the end, which is the part I marked in red
The second type: only for the current user
Modify the ". VIMRC" file in the user's home directory, note that there is a point, which is hidden files, the general user home under No, need to manually create their own
Transfer from http://www.apelearn.com/bbs/thread-9334-1-1.html
Vim Common shortcut keys
1, note all lines: Ctrl + V j/k+ shift +i
2. Check the cursor's current man manual shift+k
3,d+f+ characters
4, check area v+t+ characters
5. Delete all characters after the cursor in VIM shift+d
6, delete the word on the current cursor d+i+w
7, select the word on the current cursor v+i+w
Transfer from http://www.apelearn.com/bbs/thread-407-1-1.html
Vim quickly deletes a specified segment of characters
Because not a line, so with DD not, but with X to remove, and too slow. Today a special quick removal method is found online.
That is, using DA, how to use it, see the example below.
For example, my 1.txt content is as follows:
11111111111111111111111111
222222222222222222222222222222b3nzac1yc2eaaaabiwaaaqeav5ojvuidaavusdoa2fbfnl0k2gbtc05yg6tgm+ 8snlei6bu5mhay2up5j4ycrmu43911hej2uh1upycwx1o4xpegum8tgis1hoqysnukv3g121uolacrj37qql9j4rrhruxhunaw3allsgiv0mxfd0apyycfola /1i3hu7yyx7tdripwz0fehhht3qjfe9yc8z6ptq7cvbpxbbvc/g8pxvq3bngmtj9ifmbh7nntvfhnezgacf2mr4fsy0mmunl0k3x5sblsyp9/ rxy9cpoh73ekuhzqok3uwjwudrp/dqrxgwdveg0nz+0t130pku/ lsreothwovbu54rrtuuidb3sq0xsw4x9ehkgjjhpvbrbgbidptkbuahdqefmqqpaweex1hmc7lcunnfgtzf39pv/ 2vpxz2l8nh2jem0nrs48a6sf4efz5viakorysmqu/6my4s9au3arbx+jvue9s2/7d+jdqjlintqqru4v92lqq3bjasmmkiwnpsytxdtari3+8i 2xxqfcj5bby7e3333333333333333333333
44444444444444444444444444444
I now want to remove the characters between 22222222222 and 33333333333333333 only to do this:
- Change the contents of the 1.txt to:
11111111111111111111111111
222222222222222222222222222222{b3nzac1yc2eaaaabiwaaaqeav5ojvuidaavusdoa2fbfnl0k2gbtc05yg6tgm+ 8snlei6bu5mhay2up5j4ycrmu43911hej2uh1upycwx1o4xpegum8tgis1hoqysnukv3g121uolacrj37qql9j4rrhruxhunaw3allsgiv0mxfd0apyycfola /1i3hu7yyx7tdripwz0fehhht3qjfe9yc8z6ptq7cvbpxbbvc/g8pxvq3bngmtj9ifmbh7nntvfhnezgacf2mr4fsy0mmunl0k3x5sblsyp9/ rxy9cpoh73ekuhzqok3uwjwudrp/dqrxgwdveg0nz+0t130pku/ lsreothwovbu54rrtuuidb3sq0xsw4x9ehkgjjhpvbrbgbidptkbuahdqefmqqpaweex1hmc7lcunnfgtzf39pv/ 2vpxz2l8nh2jem0nrs48a6sf4efz5viakorysmqu/6my4s9au3arbx+jvue9s2/7d+jdqjlintqqru4v92lqq3bjasmmkiwnpsytxdtari3+8i 2xxqfcj5bby7e}3333333333333333333333
44444444444444444444444444444
Note that there is no line break between 2222 and {.
- Move the cursor to the first {, that is, the last 2, and then enter da{to delete all the characters in the {}.
In addition, in addition to the use of {, you can also use ",", (such as the special symbol of the pair.)
In fact, Vim has a more commonly used that is V, with V and D to delete is also very convenient:
After you open a text, press v directly, and then move the cursor to select the text, and when you are finished with the text you want, press D directly, and then delete it.
Transfer from http://www.apelearn.com/bbs/thread-842-1-1.html
Keypad issues
When using VI on the putty, when opening numlock, press the numeric key on the keypad and not enter a number, but a letter and then newline (in fact, the command mode corresponds to the upper and lower keys). Workaround
Option Terminal->features, find disable application keypad mode, select on it.
The Xshell in Xmanager 4 also has a small keyboard problem resolution of
Modify session Properties--terminal->VT mode--Initial numeric keypad mode
Select Set as normal.
Transfer from http://www.apelearn.com/bbs/thread-7215-1-1.html
File encryption
Linux uses Vim/vi to encrypt and decrypt files
First, using VIM/VI encryption:
Advantages: After encryption, if you do not know the password, you can not see the plaintext, including the root user can not see;
Cons: Obviously let others know encryption, it is easy for others to destroy the encrypted files, including content destruction and deletion;
VI Editor believe that everyone is familiar with it, VI has a command is to encrypt the file, for example:
1) First create an experimental file under the root master directory/root/Text.txt:
[Email protected] ~]# Vim/vi text.txt
2) Enter the edit mode, press ESC after entering the content, then enter: X (note is the uppercase X), enter;
3) Then the system prompts you to enter the password, 2 times, as follows:
Enter Password:
Please enter again:
4) After saving the exit, now this file has been encrypted;
5) Use cat or more to view the contents of the file, displayed as garbled; re-edit this file with Vim/vi, will prompt for the password, if the password entered is incorrect, the same will be displayed as garbled!
Note: Do not forget the password after the file is encrypted!
Second, decrypt the files encrypted with VI (provided that you know the encrypted password):
1) Open the file with Vim/vi, such as Text.txt, to enter the correct password, and then at the time of editing, set the password to empty, by entering the following command:
: Set key=
Then directly enter, after saving the file, the file has been decrypted.
2) or This is OK:
After opening the file correctly with ": X" instruction, and then give a blank password can also. Save with "wq!" Save.
The two methods actually have the same effect.
Transfer from http://www.apelearn.com/bbs/thread-7750-1-1.html
Vim Paste Code Auto indent cause all messed up
Use VIM to open a blank document, and then paste the copied code into it, found that it has automatic indentation function, resulting in the pasted text line is more than one line on the right, looks mess. The quicker solution is to enter the document in command-line mode before pasting it
:set noai nosi
Then press ' I ' to enter edit mode, and then paste the copied code content so that it does not automatically indent.
Sometimes, such a method is not good to use, you can try this:
: Set Paste
Transfer from http://www.apelearn.com/bbs/thread-6753-1-1.html
Linux Learning Notes (18) Vim (ii), edit mode, vim practice