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
命令,把寄存器中的內容粘貼到新的文檔中。