[Original] Summary of Vim's Practical Skills

Source: Internet
Author: User

Vim is a magical and complex Editor, which makes me a newbie in Linux programming. Every time we get to the real hands-on programming, we find that we don't know how to operate Vim at all, and we don't feel the power of VIM at all. Haha ~~ I learned a little Vim operation skills today. By the way, I will keep improving my experience in the future.

1. Copy, cut, and paste

(1) InIn command modeMove the cursor to the start position of the segment to be copied and press the letterVEnter the visual mode, move the cursor to overwrite the content to be copied, and pressYComplete the copy and pressDCut. Move the cursor to the target position and press the letter.PPaste.

(2) InIn command mode, Press y twice in the target line, that isYYThe row is copied. The two letters D, that isDdCut the line, move the cursor to the target position, and press the letterPPaste.

(3) InIn command mode, Enter the command": Start line number, end line number yYou can copy multiple rows in a row, and then move the cursor to the target position and pressPPaste.

(4) InIn command mode, Enter the command"N1, n2w! /Tmp/tmpfile"You can write the content from row N1 to row N2 to the/tmp/tmpfile file for temporary saving, move the cursor to the target location, and enter"R/tmp/tmpfileRead the content.

(5) InInsert mode, Move the cursor to the place where you want to insert the content, then use the mouse to select the content to be copied, and press the middle mouse button to copy and paste the content. The position of the paste is the position of the cursor just now.

I. Code comments

(1) continuous multi-row comments

Enter the following command in the command line:

: Start line number, end line number S/replaced character/g

For example, you can enter the following command when inserting # At the beginning of line 10th to line 25 of the file:

:10,25s/^/#/g 
:10,25s/^/\/\//g

The ^ symbol indicates the meaning of the first line insertion. Because/is used in the command, two escape characters "\" must be used to input "//". G indicates that you do not need to confirm each replacement. If you want to confirm, replace g with C.

(2) Random Comments of discontinuous rows

If you want to comment out discontinuous rows at will, you need to use the shortcut key. For example, you can add a shortcut key for the first line comment action. The command to add a shortcut key is:

: Map ^ p I //

^ P indicates Ctrl + P. Press Ctrl + V + P continuously when entering this command. Of course, you can set other letters without P. I // insert at the beginning of the row where the cursor is located. To cancel this shortcut, follow these steps:

: Unmap ^ P

Ii. Automatic string completion

You can define a special string to complete, which is sometimes very convenient. Set the replacement statement as follows:

: AB full string

For example, you can set a shortcut string to KLH for my mailbox. After Entering KLH each time, press ENTER or space to change it to my mailbox address:

:ab KLH [email protected]126.com

3. execute system commands

In vim, you can directly execute shell commands without exiting vim. The execution method is to input in command mode first !, Then enter the command. For example, display the current working path:

:!pwd

If you want to import file content from an external file, you can use the R command, for example:

:r ../echoServer/main.cpp

By extension, we found that two commands can be used together in the form of R! System commands. For example, insert the current system time into the file:

:r !date

4. View System Help

On the system function to be queried, press the letterKTo the Help file. This is a method.

5. Auxiliary Functions

(1) directory browsing, input: Ex

(2) Split Window directory browsing, input: Sex

(3) display the current buffer status, input: Ls

(4) display the currently opened file, enter: ARGs

(5) move the cursor back to Ctrl + C + P, move the cursor forward to Ctrl + C + I, and list the jump history: Ju

(6) If you need to save the above personalized settings, write them directly ~ /. Vimrc file.

 

[Original] Summary of Vim's Practical Skills

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.