[Practical.vim (2012.9)]. DREW.NEIL.TIP94 Study Summary

Source: Internet
Author: User
Tags arithmetic html header

Perform arithmetic on the replacement

Suppose we have a document like this

We want to promote each heading, turning into , into , and so on
We're going to turn every heading, for example , to a 1 reduction in numbers.

Here's the general Idea:we write a pattern that matches the numeral portion of the HTML header tags. Then we write a substitute command, uses a Vim script expression to subtract one of the from the number that is captured.
The general approach is as follows: We write the header tag of the HTML in the pattern matching file, and then use the Substitute command to subtract 1 from the found number using the Vim script.

The Search Pattern

The only thing that we want to change are the numeral part of the header tags, so ideally we want to create a pattern that Matches and nothing else. We don ' t want to match all digits. We only want to match the ones that immediately follow or . This pattern should do the trick:
What we want to change is the number in the head tag, so we're going to create a pattern that matches only to this and not the other numbers. We want to match the numbers directly followed or followed. Pattern is as follows

 /\v\<\/?h\zs\d

The \zs item allows us to zoom in on part of the match. To simplify our example, we could say that a pattern of h\zs\d would match the letter "H" followed by any digit ("H1," "H2," and so on). The placement of \zs indicates that the "H" itself would was excluded from the match, even though it's a integral part of The broader pattern (we met the \zs item in Tip, where we compared it to Perl ' s positive lookbehind assertion).

\zs allows us to locate a part of match more precisely. Simply put, h\zs\d you can match any number that follows the H. The use of \zs means that H is excluded from the match result, although H is part of the pattern. \zs means match starts, and \ze represents the end of match (described in detail in Tip77).
Also note here \<\/to < and/transpose, if not/transpose, then this/as the pattern of the termination, only search <
? represents 0 or 1 characters or a number

The Substitute Command

We want to perform arithmetic inside the replacement field of our substitute command. To does this, we'll have the to evaluate a Vim script expression. We can fetch the current match by calling the Submatch (0) function. Since our search pattern matched a digit and nothing else, we can expect that Submatch (0) would return a number. From this, we subtract one and return the result to being substituted in place of the match.
This substitute command should work:
We intend to perform arithmetic operations in the substitute command using the Vim script expression, which is introduced with \=. We can get the current match by calling Submatch (0). Since our search matches only numbers, we can expect Submatch (0) to return a number, then subtract 1 and replace the match with this value.

:%s//\=submatch(0)-1/g

Execute this command to get

[Practical.vim (2012.9)]. DREW.NEIL.TIP94 Study Summary

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.