Vim Advanced operation

Source: Internet
Author: User

This page collects some simple VIM operations that either cannot be done by other ordinary text editors or is slow to complete. Through this web page, you can be determined to learn VIM's determination and confidence. If you have any good easy-to-demonstrate tips, please contact me to add more content to this page.

In addition, the keys mentioned in this article are case-sensitive if not explicitly stated. For example, when you mention "press G", the key you press should be "Shift + G".

1 Preparatory work

First, we open VIM and enter a text for today's demo:

This is a test
2 Finding replacements

Press a few ESC to enter Normal mode and enter the following command: :%s/ /\r/g . The following results are obtained after carriage return:

Thisisatest

Explanation: The purpose of this command is to replace all the spaces in the article with a carriage return. Almost all editors support find replacements, but not all editors support replacing spaces with carriage returns, so this feature is cumbersome to do in many other editors.

3 Rows of stitching

Just now we have broken a line of words into 4 lines, then how to put them together? Of course, we can use the previous way to find and replace, and then replace the carriage return to a space to achieve the concatenation of the row. But here we are using a different approach.

Press a few ESC to enter Normal mode and enter this command: ggVG . Represents the gg jump to the beginning of the text, which V indicates entering the line selection mode, G indicating selection to the end of the article. With these 3 commands, a total of 4 keys, we have selected the whole article.

Then, press the colon : into the command mode, the status bar appears: The :‘<,‘> word, after it entered j , and then enter, you can see, the entire article was joined together, the entire operation including the carriage return only pressed 7 times the key:

This is a test
4 copying and pasting and repeating actions

Press a few ESC to confirm that you are currently in Normal mode, and then press to yy copy the current line to the default register (equivalent to the clipboard). Then press 12p , VIM will perform the paste action 12 times, the screen appears 13 lines such characters:

The is a testthis are a testthis is a testthis are a testthis is a testthis are a testthis is a testthis are a testthis is a Testthis is a testthis are a testthis is a testthis is a test

Explanation: In VIM, copy and paste operations are fairly quick. In addition, most of the commands in VIM can be repeated several times by adding numbers before the command.

5 Column Operations

Next we change the first letter of each line to uppercase.

Press a few ESC to confirm that the current is in Normal mode, then press gg Ctrl + V to enter the column selection mode (if you press CTRL + V not to enter Column selection mode), then press G , jump to the last line of the article, and you should see the text The first column is selected, and only the first column is selected. By pressing the U key, you can see that the first letter of each line becomes uppercase. Tip: When you select text u , press to change the text to lowercase, and then press ~ to flip the original case when the text is selected.

The is a testthis are a testthis is a testthis are a testthis is a testthis are a testthis is a testthis are a testthis is a Testthis is a testthis are a testthis is a testthis is a test

We then add an asterisk to the front of each line. Press gg to jump to the first line, press Ctrl + V to enter the column selection mode, press, G Select the first column of the full text, then press I , enter the column insert state, enter * an asterisk, and then press ESC, and you will see that all lines have an asterisk before it:

*This was a test * this is a test *This is a test * this is a test *This is a test *<
   
     C5>this is atest * The is a test *
    This is a test *
    This is a test *
    This is a test *
    
      0>this is atest * This was
     a test*this is a test
    
   

Explanation: For people who write programs, it is a common practice to annotate a piece of code in bulk, and using column insertion makes it easy to do so. In addition, by pressing X to delete the selected block after column selection, you can dismiss the comment in batches.

6 macro Recording

Next, we'll modify the even lines of the text to: This is another test . Since all the even lines are doing the same thing, we record the operation and then repeat it several times to finish the work quickly.

First, press a few ESC to confirm that it is in Normal mode, and then press gg to jump to the first line and prepare to start the operation. We first press the q key and then press another letter to record the macro to the register that corresponds to the letter. For example, we use m registers here, then press qm . The VIM status bar appears with the word "recording", indicating that it has entered the recording state.

Then, we change the second line a to another . First press to j enter the second row, then press $ to jump to the end of the line, and then press two to b jump forward two words, at which point the cursor stops on the letter a . Then we press the caw key to delete a and go into the Insert State, then enter another , press ESC to return to Normal, press J to enter the next line, the entire operation step is completed. Finally, we click again q to end the recording of the macro.

Next we play the macro and complete the procedure. Typing on the keyboard [email protected] means that the macro in the M register is played 1000 times, and immediately you can see that all the even lines in the article a become another .

* This wasa test* This isanother test* This isa test* This isanother test* This isa Test* This wasanother test* This isa test* This isanother test* This isa test*
   
    this is another test* This is
    a test* This is
    another test*this is a test
   

Explanation: Although we specified to play 1000 times, but in fact, the execution to the 6th time, the cursor moved to the bottom of the screen, so the execution process automatically stopped. Therefore, in bulk operations, we can specify large enough numbers without worrying about problems.

In addition, a when modifying, we jump to the last line and then use the b command to jump in words, instead of using a h letter to move back, we use to caw modify the whole word without using a command to s delete a single letter and enter Insert mode 。 These details ensure that the recorded macros are more general.

7-line End block operation

Note: This chapter is contributed by Jason Han netizens, thanking him for his letter pointing out that the original operation of the Dian Fox was understood by the end block.

Below, we will add an exclamation mark at the end of each line. Before we added an asterisk to the head of each line, we used the ctrl-v column operation. Now to add at the end of the line, can you continue with the column operation? Intuitively it doesn't seem to work, the length of each line is different, the end of the line is uneven, how to use the column mode to add things to the end of the line?

In fact, Vim provides a special column pattern called the line-end block pattern, that is, we can select the line end of a different length line through the ctrl-v mode, and then unify the end of the line, the following steps:

Press gg to jump to the first line, press Ctrl + V to enter the column selection mode, then press, G Select the first column of the full text, then press, enter the $ line end block mode, press, enter the A block insert state, enter the asterisk ! , and then press ESC, and you will see that all the line tails appear An exclamation point:

*this is a test!*this is another test!*this are a test!*this is another test!*this are a test!*this is another test!*This are A test!*this is another test!*this are a test!*this is another test!*this are a test!*this is another test!*this is a test!
8-point Command

Next, we add a less-than sign at the end of each line < . Insert a new line below each line, and write a greater than sign > .

Since we need to add a new line after each line, we cannot bulk add less than greater than by using the block selection method. You can do this by using macro recording, but the operation is a little cumbersome. This can be done very conveniently using the point command.

First press ESC to confirm that it is currently out of normal mode, then use gg to jump to the first line, press A the line end to insert, enter, < and then press ENTER, enter > , and finally ESC back to Normal state, the first line of modification is complete.

Then, we press into the next line, that is, the j third row, and then press . , you can see that the third line of the tail also appeared less than the sign, and automatically added the fourth line of greater than sign. Press repeatedly j.j.j. until each line is finished with this edit action.

*this is a test!<>*this are another test!<>*this is a test!<>*this are another test!<>*this is a tes T!<>*this is another test!<>*this are a test!<>*this is another test!<>*this are a Test!<>*Thi S is another test!<>*this are a test!<>*this are another test!<>*this is a test!<>

Explanation: The point command is the function of repeating the last edit operation. Because the action in the first line is to add and insert a new row at the end of the line, the same character is added at the end of the line when the third row (the previous second row) repeats the action. The point command function is not as powerful as a macro, but it is easier to use than a macro and therefore has a wide range of uses.

Vim Advanced operation

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.