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

Collect TODO Items in a Register

Combining the :global and :yank commands allows us to collect all lines that match a {pattern} in a register.
This excerpt of code contains a couple of comments that lead with “TODO ”:
结合:global:yank命令可以把匹配{pattern}的所以行复制到register中,下面代码里面有许多注视都是以TODO开头的。
这里写图片描述

Suppose that we wanted to collect all of the TODO items in one place. We could view them all at a glance by running this command:
假设我们打算收集所有TODO项到一个地方,我们可以执行下面命令查看下他们:

 :g/TODO
// TODO: Cache this regexp for certain depths.
// TODO: No matching end code found - warn!

Remember, :print is the default [cmd] for the :global command. This simply echoes each line containing the word “TODO. ” It’s not a great deal of use though, because the messages disappear as soon as we execute another command.
记住,:global命令的[cmd]的默认命令就是:print.这次简单的把包含TODO的每一行打印出来了。并不是很好的做法,因为当我们执行下一条命令的时候这些消息就消失了。
Here ’s an alternative strategy: let’s yank each line containing the word “TODO ” into a register. Then we can paste the contents of that register into another file and keep them around for later.
另外一个方法就是可以把包含TODO的每一行复制到寄存器中,然后把这个寄存器中的内容粘贴到其他文件中,就可以一直保存了。
We ’ll use the a register. First we ’ll need to clear it by running qaq . Let’s break that down: qa tells Vim to start recording a macro into the a register, and then q stops the recording. We didn ’t type anything while the macro was recording, so the register ends up empty. We can check that by running the following:
我们要使用寄存器,首先要把寄存器中的内容清空。可以用命令qaq实现。把这个命令分开,:qa告诉Vim开始记录宏到寄存器,q告诉Vim停止记录。记录期间我们没有输入任何字符,所以寄存器最终是空的。我们可以用下面的命令来检查一下。

:reg a
 --- Registers ---
"a

Now we can go ahead and yank the TODO comments into the register:
接下来我们继续,把TODO的内容复制到寄存器:

 :g/TODO/yank A
 :reg a
❮ "a // TODO: Cache this regexp for certain depths.
// TODO: No matching end code found - warn!

The trick here is that we ’ve addressed our register with an uppercase A. That tells Vim to append to the specified register, whereas a lowercase a would overwrite the register’s contents. We can read the global command as “For each line that matches the pattern /TODO/, append the entire line into register a. ”
这里巧妙的地方在于我们引用我们的寄存器时使用了大写的·A ,这告诉Vim把内容添加到特定的寄存器中,小写的a 会覆写寄存器中的内容。我们可以把这条命令解释为“对每一条包含TODO的行添加到寄存器a中”
This time, when we run :reg a, we can see that the register contains the two TODO items from the document. (For the sake of legibility, I ’ve formatted these items on two separate lines, but in Vim it actually shows a ^J symbol for newlines.) We could then open up a new buffer in a split window and run "ap to paste the a register into the new document.
这次我们执行命令:reg a ,我们可以看到寄存器包含文档中的两条TODO项。我们可以在一个新窗口打开一个新的buffer,然后执行"ap 命令,把寄存器中的内容粘贴到新的文档中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值