Vi/vim Calculation of search keywords

Source: Internet
Author: User

Http://hi.baidu.com/xletian/blog/item/f19962061a9a506c020881dc.html

After looking at VIM's global command and search instructions, will you also think you have what you need to be called Vim?

Today, I talked to my elder pudding elders and DK Elders about a question: Can I know how many strings I want to search for in this document?

Of course, if you could. The global command of vim you may be able to use: after g/pattern/p, use copy or other instructions to save the object to another file, and then use other tools (like WC) to calculate the number of rows. However, this method is not convenient enough.

The more laborious way also can use/pattern and then oneself press n each calculate, but ... I think it's going to be crazy first.

is vim the only way to do that?

Forget is DK Elder or pudding Elder say can write program to calculate. But Vim's script, I'm going to write it back. My mind is that there should be a line to solve the method Ah!

There are two ways to encounter problems in Vim, and the first one is: h then follow the keywords that you guessed might have the answer. But this way I used a few times today, I feel that there is no other ways to write the program. But I just don't want to write a program. The second method is to look for tips on the www.vim.org, the result is not two to find this article <tip #860: Count number of matches of a pattern> Just think, "Hey yo, how just didn't think?" "In fact, it's interesting to use Vim to edit a file to think about how this can be a little less or just a single line of instructions." The person used the method is very simple and beautiful, but to use the substitution instructions we mentioned earlier: s:

  :%s/pattern/&/g

  The meaning of the & is that it is used to denote a string of previous pairs, so the instructions do not change anything in the file itself. But because it's a global substitution permutation, Vim tells you how many strings are replaced by the number of lines. It's nice to solve this problem with a single line of command.

Enter a command with only one line:

The result will appear on the last line and will not change to the file itself:

Vi/vim Calculation of search keywords

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.