Perform Arithmetic on the Replacement

假设我们有一个文档如下

We want to promote each heading, turning <h2> into <h1>, <h3> into <h2>, and so on
我们打算把每一个heading 比如 <h2>转为 <h1>,数字降低1.

Here ’s the general idea: we write a pattern that matches the numeral portion of HTML header tags. Then we write a substitute command that uses a Vim script expression to subtract one from the number that was captured.
一般的方法如下:我们写一个pattern匹配文件中HTML的头tag,然后再用substitute命令,通过vim 脚本把找到的数字减去1.

The Search Pattern

The only thing that we want to change is the numeral part of the header tags, so ideally we want to create a pattern that matches that and nothing else. We don ’t want to match all digits. We only want to match the ones that immediately follow <h or </h. This pattern should do the trick:
我们想改变的是head tag中的数字,所以我们要创建一个pattern只匹配到这个而不是其他的数字。我们要匹配直接跟在<h</h后面的数字。pattern如下

 /\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 be excluded from the match, even though it is an integral part of the broader pattern (we met the \zs item in Tip 77,, where we compared it to Perl’s positive lookbehind assertion).

\zs可以让我们更加精确的定位到match的某个部分。简单的说h\zs\d 可以匹配跟在h后面的任何数字。采用\zs意味着h从match结果中排除掉了,尽管h是pattern的组成部分。\zs表示match开始,\ze表示match结束(在Tip77中有详细描述)。
同时注意这里\<\/ 对<和/进行了转置,如果不对/进行转置,那么会把这个/作为pattern的终止,只搜索<.
?表示0或1个字符或数字

The Substitute Command

We want to perform arithmetic inside the replacement field of our substitute command. To do this, we ’ll have 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) will return a number. From this, we subtract one and return the result to be substituted in place of the match.
This substitute command should work:
我们打算在substitute命令中执行算术运算,就要采用vim script表达式,用 \=来引入。我们可以通过调用submatch(0)来获取当前的匹配。由于我们的搜索只匹配出了数字,所以我们可以期望submatch(0)返回数字,然后可以减去1,把这个值替换match.

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

执行这个命令得到

[Practical.Vim(2012.9)].Drew.Neil.Tip94 学习摘要相关推荐

  1. [Practical.Vim(2012.9)].Drew.Neil.Tip04学习摘要

    Find and Replace by Hand 在上面文本中,我们打算把content 更改为单词copy 也许我们可以用如下命令 :%s/content/copy/g 上面命令把文本中所有的con ...

  2. [Practical.Vim(2012.9)].Drew.Neil.Tip10学习摘要

    Use Counts to Do Simple Arithmetic 在vim中,执行<C-a>和<C-x>命令可以对文本中的数字直接进行加或减. 如果不提供数字而直接执行上面 ...

  3. [Practical.Vim(2012.9)].Drew.Neil.Tip20学习摘要

    Visual mode allows us to select a range of text and then operate upon it. However intuitive this mig ...

  4. [Practical.Vim(2012.9)].Drew.Neil.Tip19学习摘要

    Overwrite Existing Text with Replace Mode Replace模式和insert模式类似,不过是覆盖文档中的已有字符.例如我们要修改文中的一个段落,可以输入R,进入 ...

  5. [Practical.Vim(2012.9)].Drew.Neil.Tip16学习摘要

    Do Back-of-the-Envelope Calculations in Place 在vim中我们可以利用表达式寄存器来直接进行数学运算然后把结果插入我们的文档. 表达式寄存器可以通过=来访问 ...

  6. [Practical.Vim(2012.9)].Drew.Neil.Tip12学习摘要

    Operator+Motion=Action 在Vim中,d{motion}命令可以删除单个字符dl,也可以删除整个单词daw,整个段落dap. 同样的可以用c命令来改变单词caw或锻炼cap.y命令 ...

  7. [Practical.Vim(2012.9)].Drew.Neil.Tip09学习摘要

    Compose Repeatable Changes 在vim中实现一个操作可以有许多方法,但是选择执行步骤最少的方法可以让这个方法更具重复性. 如下文本我们要删除nigh. 1)Delete Bac ...

  8. [Practical.Vim(2012.9)].Drew.Neil.Tip04 学习摘要

    在Vim中执行一些重复命令时,可以用一些快捷键代替.如下列表 编辑文本然后退出到Normal模式后,.重复上次改变操作,如果多按了一次.,我们可以使用u命令来撤销上一次的重复. 使用f/F查找字符后, ...

  9. [Practical.Vim(2012.9)].Drew.Neil.Tip01 学习摘要

    Meet the Dot Command The dot command lets us repeat the last change. It is the most powerful and ver ...

最新文章

  1. Dos攻击工具(ZAmbIE)
  2. 因4元而市值蒸发400亿!美团“大数据杀熟”翻车!回顾2020年互联网大事件!...
  3. tensorflow gan 网络 示例
  4. java interface泛型_java – 使用泛型和接口实现组合
  5. 完整的目标管理三段俱全
  6. sql 创建用户脚本
  7. leveldb使用指南
  8. c语言信息管理系统排序怎么编,C语言编职工信息管理系统怎么做?
  9. JQuery Mobile - 处理图片加载失败!
  10. 95-130-022-源码-source-基于socket的source源码分析SocketTextStreamFunction
  11. 计算机组成原理测试题
  12. android 字体像素转换工具类_Android点9图机制及在聊天气泡中的应用
  13. Python大数据分析(一):认识大数据
  14. 重读模电教材一_童诗白、华成英
  15. APP应用平台有哪些?
  16. 王之泰201771010131《面向对象程序设计(java)》第二周学习总结
  17. 计蒜课:Fantastic Graph(图论)(有源汇的上下界的可行流)
  18. java两数相乘的函数_jquery 加法 乘法运算 精确计算函数
  19. 余三码 2020-12-30
  20. 这可能是最完整的进藏攻略

热门文章

  1. 修改cmd编码为UTF-8
  2. ssm基于Java的微小企业人事管理系统的设计与实现毕业设计源码231012
  3. 仿QQ多级折叠、展开菜单,三级下拉导航
  4. 基于NS模型+久期衡量利率风险(Julia)
  5. 软件开发V模型--解读
  6. 我的世界服务器显示弹幕,我的世界直播弹幕模组
  7. 液晶面板里面有些什么配件_液晶模组LCM和液晶面板有什么区别
  8. 作业帮、猿辅导押注的下沉市场未必是在线教育企业的好市场
  9. hiho1718(LIS)
  10. 虚拟机的介绍(超详细)