I. Introduction of VIM
Vim is an upgraded version of VI: Available with color display
1. Install Vim
Yum Install-y vim-enhancedvim/etc/passwd
2. Three modes:
General mode: DD P yy edit mode: Edit file Content Command mode::/?
Two, Vim color display and move cursor 1. Vim can display colors based on the path and name of the file
650) this.width=650; "Src=" http://note.youdao.com/yws/api/personal/file/965D4E38E936493DA3B988C72C9A95E3?method= Download&sharekey=4e5799e929421f2ba899499860f7d4f2 "alt=" image "style=" border-style:none; "/>
Cp/etc/passwd/tmp vim/tmp/passwd View there is no color/etc will show the color, is his characteristics. Cp/etc/fstab/tmpvim/tmp/fstab View there is a color display. VIM/TMP/PASSWD added "#/test" There is color,/tmp/passwd renamed to passwd.sh on the display color.
2. configuration file:/ETC/VIMRC
VIM/ROOT/.VIMRC configuration automatic indentation, syntax highlighting and other functions. Cp/etc/dnsmasq.conf/tmp/1.conf in the first line: #!/bin/bash
3. Move the cursor in general mode:
H or LEFT arrow: Move left. J or DOWN ARROW: 5j Move down 5-row K or up ARROW: Move up L or right arrow: Move right to space: Move right 5+ space: Move 5 characters to the right
Third, VIM general mode to move the cursor
CTRL +: Move one word to the right Ctrl + B or pageup: Page Up Ctrl + F or PageDown: page back 0 or ^: move to the beginning of $: Move to line end GG: Navigate to the first line G: Navigate to the tail line 50G: Navigate to Line 50
Four, the VIM general mode copy, cut and paste
x: Lowercase Backward Delete one character x: uppercase: Delete a character forward DD: Delete cursor row 10DD: Delete (contains cursor row) 10 lines P: (lowercase) paste Y on the line of the cursor: (uppercase) Paste the NYY: Copy the cursor under N lines u: Undo, Up to 50 times CTRL + R: Reverse Undo V: Visualize, check (as you wish)
Five, enter the editing mode
I: Insert before cursor I: position to the beginning of the line insert O: Insert o: On the line where the cursor is inserted a: Insert a: Insert after the cursor at the end of the row
Vi. VIM Command mode
/word back from the cursor Wordn: Look down N: Look up? Word queries forward from the cursor Wordn: Look up N: down: 1,100s/word1/word2/g replace 1 to 100 rows word1 for word2:1, $s/dnsmasq/ Fxq/g No G time only replaces the first of the lines: 1,[email protected]@[email protected]:w:w!:q:wq Save: X Save if the file is only open. Wq only changed the atime.x used without Atmie:q!:wq!:set nu display line number: Set Nonu does not display line number: Nohl does not display search highlighting
Seven, Vim practice
650) this.width=650; "Src=" Http://note.youdao.com/yws/api/personal/file/76F7F7EF25C24289A26E739FE8B6AE29?method =download&sharekey=d3a48e1b78b2b6ca0723ba07270e292b "alt=" image "style=" border-style:none; "/>
650) this.width=650; "Src=" http://note.youdao.com/yws/api/personal/file/WEBe2ec31a0512e836bd04f40199a0dd138? method=download&sharekey=6e2985caed0a62262886ccdf6fa1999d "alt=" image "style=" border-style:none; "/>
Eight, Vim extension:
Some unknown operations in the VIM editor
The replacement mode of the 1.vim editor and the 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 the 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 everything from 10 to 15 lines in general mode enter ": 10,15d" carriage return
6. Save the file you are editing as a new file
Example: Save the file being edited to "/root/" and save it as 1.txt enter ": W/root/1.txt" in general mode: Save the 10th line of the file you are editing to line 15th as 1.txt and save it to the root directory in general mode: 10,15 W/root/1.txt "
7. Import the contents of other files into the file where you are editing the cursor
Example: The contents of the "/root/1.txt" file, import into the next under the 10th line of the edited file, first in the general mode, press "10G" to position the cursor to line 10th and then press "O" lowercase, another line below the current line, and enter insert mode at the end of the keyboard to return to the general mode, and then enter ": R/root/1.txt "Enter
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 enter "in edit mode":! 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 "#" to the beginning of line 10 to 15 in the general mode, enter ": 10,15s/^/#/" Example: At the beginning of line 10 to 15 to remove the "#" in general Mode input ": 10,15s/^#//" Example: in the line of 10 to 15 increase "//" In general mode input " : 10,15s/////"or": 10,[email protected]@//@ "or": 10,15s#^#//# "
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" in general mode enter ": AB aming abcdefghijklmnopqrstuvwxyz" and then enter the edit mode when you enter "Aming" , you'll find that it's automatically replaced with "ABCDEFGHIJKLMNOPQRSTUVWXYZ."
12. Definition of shortcut keys
Example: I want to press the Ctrl+b shortcut on the keyboard in normal mode, automatically insert the "#" sign at the beginning of the line where the cursor is located, and then automatically exit to normal mode, enter ": Map Ctrl + V ctrl+b I #" and then return to the normal mode by pressing CTRL + on the keyboard. B will insert the "#" number at the beginning of the cursor.
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 2.txt simultaneous editing, and the window horizontally separated, together display in general mode input ": New/root/2.txt"
Example: I am now editing the 1.txt file, and then I want to open the root directory of 2.txt simultaneous editing, and the window left and right vertical separation, together display in general mode input ": Vsplit/root/2.txt"
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 and enter ": Set Noic" If you want to cancel
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. configuration file usage 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 time we can write these commands to the configuration file, so that after the restart of the computer can be used, we have two methods: All users uniformly modify the "/ETC/VIMRC" file, at the end to add the need to set the command, is my red labeled part of the second: only the current user changes in the user's home directory ". VIMRC" file, note there is a point, which is hidden files, the general user under the home does not need to manually create their own
Share some of the shortcuts commonly used in Vim 1, note all the lines: Ctrl + V j/k+ shift +i2, view cursor current man manual shift+k3,d+f+ character 4, check area v+t+ character 5, delete all characters after cursor in Vim Shift+d6, Delete the word d+i+w7 the current cursor, and select the word on the current cursor v+i+w
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. Like what My 1.txt content is as follows: 11111111111111111111111111222222222222222222222222222222B3NZAC1YC2EAAAABIWAAAQEAV5OJVUIDAAVUSDOA2FBFNL0K2GBTC 05yg6tgm+ 8snlei6bu5mhay2up5j4ycrmu43911hej2uh1upycwx1o4xpegum8tgis1hoqysnukv3g121uolacrj37qql9j4rrhruxhunaw3allsgiv0mxfd0apyycfola /1i3hu7yyx7tdripwz0fehhht3qjfe9yc8z6ptq7cvbpxbbvc/g8pxvq3bngmtj9ifmbh7nntvfhnezgacf2mr4fsy0mmunl0k3x5sblsyp9/ rxy9cpoh73ekuhzqok3uwjwudrp/dqrxgwdveg0nz+0t130pku/ lsreothwovbu54rrtuuidb3sq0xsw4x9ehkgjjhpvbrbgbidptkbuahdqefmqqpaweex1hmc7lcunnfgtzf39pv/ 2vpxz2l8nh2jem0nrs48a6sf4efz5viakorysmqu/6my4s9au3arbx+jvue9s2/7d+jdqjlintqqru4v92lqq3bjasmmkiwnpsytxdtari3+8i 2xxqfcj5bby7e333333333333333333333344444444444444444444444444444 I now want to delete the characters between 22222222222 and 33333333333333333 only need to do this: 1 . changed the contents of 1.txt to: 11111111111111111111111111222222222222222222222222222222{ b3nzac1yc2eaaaabiwaaaqeav5ojvuidaavusdoa2fbfnl0k2gbtc05yg6tgm+ 8snlei6bu5mhay2up5j4ycrmu43911hej2uh1upycwx1o4xpegum8tgis1hoqysnUkv3g121uolacrj37qql9j4rrhruxhunaw3allsgiv0mxfd0apyycfola/1i3hu7yyx7tdripwz0fehhht3qjfe9yc8z6ptq7cvbpxbbvc/ g8pxvq3bngmtj9ifmbh7nntvfhnezgacf2mr4fsy0mmunl0k3x5sblsyp9/rxy9cpoh73ekuhzqok3uwjwudrp/dqrxgwdveg0nz+0t130pku/ lsreothwovbu54rrtuuidb3sq0xsw4x9ehkgjjhpvbrbgbidptkbuahdqefmqqpaweex1hmc7lcunnfgtzf39pv/ 2vpxz2l8nh2jem0nrs48a6sf4efz5viakorysmqu/6my4s9au3arbx+jvue9s2/7d+jdqjlintqqru4v92lqq3bjasmmkiwnpsytxdtari3+8i 2xxqfcj5bby7e}333333333333333333333344444444444444444444444444444 Note that there is no line break between,2222 and { . 2. move the cursor to the first {, which is the last 2, and then enter the da{ to delete all the characters in the {}. In addition, in addition to the use of { , you can also use ", ", ( and other pairs of special symbols. In fact, Vim has a more commonly used that is V, with V and D to delete is also very convenient: after opening a text, press v directly, and then move the cursor can select the text, after the election of the text you want, directly press D, delete.
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 faster solution is to paste the document, in command-line mode, enter: Set Noai Nosi then press ' I ' into the edit mode, and then paste the copied code content, so it is not automatically indented. Sometimes, such a method is not good to use, you can try this:: Set paste
Client putty, Xshell connection to vim in Linux keypad problem
Reply to the collection share on the putty with VI, when the numlock on the keypad when the number keys and can not enter a number, but a letter and then a newline (in fact, the command mode corresponding to the next key). Workaround Options Terminal->features, find disable application keypad mode, select on the Xmanager in Xshell 4 also has a small keyboard problem resolution to modify session properties- > Terminal->VT mode, initial numeric keypad mode selection is set to normal.
linux encrypt and decrypt files with Vim/vi
File encryption, the use of VIM/VI encryption: The advantage: After encryption, if you do not know the password, you can not see the plaintext, including the root user can not see; disadvantage: It is obvious that people know the encryption, easy to let others to destroy the encrypted files, including content destruction and deletion; VI editor believe that everyone is familiar with it, VI has a command to encrypt the file, for example: 1) first set up an experimental file under the root master directory/root/text.txt:[[email protected] ~]# Vim/vi TEXT.TXT2) into edit mode, after entering the content, press ESC, then enter: X (note is the uppercase X), carriage return; 3) then the system prompts you to enter the password, 2 times, as follows: Enter the password: ******* Please enter again: *******4) Save and exit, Now this file has been encrypted; 5) use cat or more to view the contents of the file as garbled; re-edit this file with Vim/vi, you will be prompted to enter the password, if the password is incorrect, the same will be displayed as garbled! Note: Do not forget the password after the file is encrypted! Second, decrypt the file with VI encryption (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 when editing, set the password to empty, by entering the following command:: Set key= and then directly enter, save the file, The file has been decrypted. 2) or This is OK: after the correct opening of the file with ": X" instructions, and then give a blank password can also. Save with "wq!" Save. The two methods actually have the same effect.
This article is from the "Feng Xiaoqing blog" blog, make sure to keep this source http://fengyunshan911.blog.51cto.com/995251/1959837
Use of VIM commands in Linux CentOS7