In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this "Vim Advanced use case Analysis" article, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Vim Advanced use case Analysis" article.
1. Preparatory work
First, let's open VIM and enter a piece of text for today's demonstration:
This is a test2. Find and replace
Press ESC to enter Normal mode and enter the following command::% s / /\ r/g/. The results obtained after entering enter are as follows:
Thisisatest
Explanation: the purpose of this command is to replace all the spaces in the article with enter. Almost all editors support finding and replacing, but not all editors support replacing spaces with carriage returns, so this feature is cumbersome in many other editors.
3. Splicing of rows
Just now we broke a line of text into four lines, so how to put them together again? Of course, we can find the replacement way mentioned earlier, and then replace the carriage return with a space to achieve the splicing of the lines. But here we are using a different approach.
Press ESC a few times to enter Normal mode, and then enter this command: ggVG. Gg means skip to the beginning of the text, V means to enter line selection mode, and G means to select to the end of the article. With these three commands and a total of four keystrokes, we selected the whole article.
Then, press the colon: enter command mode, the status bar appears::', type j after it, and then enter. You can see that the whole article has been spliced together again, and the whole operation includes pressing the enter key only 7 times:
This is a test4. Copy and paste and repeat actions
Press ESC to confirm that you are in Normal mode, and then press yy to copy the current line to the default register (equivalent to the clipboard). Then press 12p _ vim to perform the paste action 12 times, and 13 lines of characters appear on the screen:
This is a testthis is a testthis is a testthis is a testthis is a testthis is a testthis is a testthis is a testthis is a testthis is a testthis is a testthis is a testthis is a test
Explanation: in VIM, copy and paste operations are fairly fast. In addition, most commands in VIM can be repeated several times by adding numbers to the command.
5. Column operation
Then we capitalize the first letter at the beginning of each line.
Press ESC to confirm that you are currently in Normal mode, then press gg to jump to the first row, press Ctrl + v to enter column selection mode (if you press Ctrl + v to enter column selection mode, see here), and then press G to skip to the last line of the article, you should see that the first column of the text is selected, and only the first column is selected. Press the U key and you can see that the first letter of each line is capitalized. Tip: select the Chinese text and press u to change the text to lowercase, and then press ~ to flip the original case.
This is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a testThis is a test
Then we put an asterisk in front of each line. Press gg to jump to the first row, press Ctrl + v to enter column selection mode, press G to select the first column of the full text, then press I to enter the column insert state, enter an asterisk *, and then press ESC, you will see that all rows are preceded by an asterisk:
* This is a test*This is a test*This is a test*This is a test*This is a test*This is a test*This is a test*This is a test*This is a test*This is a test*This is a test*This is a test*This is a test
Explanation: it is a common operation for programmers to comment out a piece of code in batches, which can be easily done using column inserts. In addition, after column selection, press x to delete the selected blocks, which can be uncommented in batches.
6. Recording of macros
Next, we will change the even lines of the text to: This is another test. Since all even lines do the same thing, we can record this operation and play it over and over again several times to get the job done quickly.
First, press ESC to confirm that you are in Normal mode, and then press gg to jump to the first line, ready to start. We first press the Q key, and then press another letter to record the macro to the corresponding register. For example, if we use the m register here, press qm. At this point, the word "recording" appears in the VIM status bar, indicating that you have entered the recording state.
Then, we change the an in the second line to another. First press j to enter the second line, then press $to jump to the end of the line, and then press b to jump forward two words, when the cursor stops over the letter a. Then we press the caw key to delete an and enter the insert state, then type another, press ESC to return to the Normal state, press j to proceed to the next line, and the whole operation is completed. Finally, we press Q again to end the recording of the macro.
Next, let's play this macro and complete the whole procedure. Typing 1000cm on the keyboard means that the macro in the m register is played 1000 times, and you can immediately see that all the even lines of an in the article have become another.
* This is a test*This is another test*This is a test*This is another test*This is a test*This is another test*This is a test*This is another test*This is a 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, in fact, when the execution reached the 6th time, the cursor moved to the bottom of the screen, so the execution process stopped automatically. Therefore, in batch operations, we can specify numbers that are large enough without having to worry about problems.
In addition, when we modify a, we skip to the end of the line and use the b command to jump by word, instead of using h to move back, we use caw to modify the entire word instead of using the s command to delete a single letter and enter Insert mode. These details ensure that the recorded macros are more general.
7. End-of-line block operation
Note: this chapter is contributed by Jason Han netizens. Thank him for his letter pointing out the mistake of Dianhu's original understanding of the operation of the end-of-line block.
Next, we will add an exclamation point at the end of each line. Previously, when we added an asterisk to the header of each row, we used the Ctrl-V column operation. Now that you want to add it at the end of the row, can you continue with the column operation? Intuitively, it does not seem to work, the length of each row is different, the position of the end of the row is uneven, how to use the column mode to add something to the end of the row?
In fact, Vim provides a special column mode called row end block mode, that is, we can select the end of a row of different lengths through the Ctrl-V mode, and then uniformly manipulate the end of the row. The steps are as follows:
Press gg to jump to the first row, press Ctrl-V to enter column selection mode, press G to select the first column of the full text, then press $to enter block mode at the end of the row, press A to enter the block insert state, enter the asterisk!, and then press ESC, you will see that an exclamation point appears at the end of all lines:
* This is a tests.This is a test.This is another test.This is a test8. Point command
Next, we add a less than sign at the end of each line.
Because we need to add a new line after each line, we cannot use block selection to add the less than than sign in bulk. This can be done using macro recording, but the operation is a little tedious. This can be done very easily with point commands.
First click ESC to confirm that you are in Normal mode, then use gg to jump to the first line, press A to insert at the end of the line, enter, and finally ESC to return to the Normal state, and the first line modification is completed.
Then, we press j to proceed to the next line, the third line, and then press., you can see that the less than sign appears at the end of the third line, and the greater than sign of the fourth line is automatically added. Press j.j.j repeatedly. Until each line completes this editing action
* This is a testers, this is another testings, this is a testings, this is another testings, this is a test!
Explanation: the point command repeats the most recent editing operation. Because what you do in the first line is to add and insert a new line at the end of the line, the same character is added at the end of the line when the action is repeated on the third line (the original second line). Dot commands are not as powerful as macros, but they are easier to use than macros, so they have a wide range of uses.
The above is about the content of this article on "Advanced use case Analysis of Vim". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.