您的位置:首页 > 其它

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

2015-04-19 23:42 351 查看

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
命令,把寄存器中的内容粘贴到新的文档中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pattern todo global