Some tips for vim

Source: Internet
Author: User

1.vim Editor replacement mode and visual mode
In general mode, press theRAndREnter 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 big R then all the characters you enter below will be replaced by the following characters until you pressexits the replacement mode.

in general mode, press thevand theVenter the visual 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 in general mode "D"or enter"d$”

3. Remove the character from the cursor to the beginning of the line


Enter uppercase in general mode "d^”


4. Delete the line from the cursor line to the end of the file


Enter uppercase in general mode "DG”


5. Delete all rows in the specified range

Example: Delete all contents from 10 to 15 rows
In general mode, enter: 10,15d"Enter

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
In general mode, enter: W/root/1.txt”
Example: 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, enter ": 10,15 w/root/1.txt”

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 the10G"Position the cursor to line 10th
and then press "o"Lowercase, another line below the current line, and into insert mode
At the end of the keyboard, pressReturn to normal 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
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

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
In general mode, enter: 10,15s/^/#/”
Example: Remove "#" at the beginning of line 10 to 15
In general mode, enter: 10,15s/^#//”
Example: Add "//" at the beginning of line 10 to 15
In general mode, enter: 10,15s/^/\/\//Or: 10,[email protected]^@//@Or: 10,15s#^#//#”


Note:On the last side of all the commands above, you can addgOrCIn conjunction with, 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 the character, then can not appear after the 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 frequently enter "abcdefghijklmnopqrstuvwxyz"Such a continuous string, this time 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 replace" aming "to"abcdefghijklmnopqrstuvwxyz”
In general mode, enter: AB aming abcdefghijklmnopqrstuvwxyz"Then enter in the edit mode, and when you type" aming "you will find that it is automatically replaced by"abcdefghijklmnopqrstuvwxyz”

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 #"Then enter, when you press the ctrl+b on the keyboard to insert the" # "sign at the beginning of the cursor.

Note:In the commandCtrl + V and Ctrl+bKeyboard is a combination of keys, not input characters, is required to press the combination of keys, 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
In general mode, enter: New/root/2.txt”

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
In general mode, enter: Vsplit/root/2.txt”

Note: In general mode, press thectrl+w"Key combination, then press the left or right, or up and down ARROW keys, you can switch between different windows if you enter in general mode": only"Then only the window that is currently being edited is left, and all others are closed


15. Case insensitive when vim looks up keywords

In general mode, enter: Set IC”
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

In general mode, enter: Set readonly”

17. Restore the file to its open state

In general mode, enter: e!”

.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/ETC/VIMRC"File, add the command that needs to be 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


Some tips for vim

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.