Collect TODO Items in a Register
Combining The:global And:yank commands allows us to collect all lines this match a {pattern} in a register.
This excerpt of code contains a couple of comments that leads with "TODO":
The combination :global
and the :yank
command can copy the line that matches {pattern} into the register, and many of the following code are prefaced with Todo.
Suppose that we wanted to collect all of the TODO items in one place. We could view them all @ A glance by running this command:
Suppose we are going to collect all todo items into one place, we can execute the following command to view them below:
:g/TODO// TODO: Cache this regexp for certain depths.// TODO: No matching end code found - warn!
Remember,:p rint is the default [cmd] for the:global command. This simply echoes all 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.
Remember: GlobalThe command's [cmd] default command is:p rintThis time, it simply prints out each line that contains TODO. It's not a good idea because the message disappears when we execute the next command.
Here's an alternative Strategy:let's Yank each line containing the word "TODO" into a register. Then we can paste the contents of this register into another file and keep them around for later.
Another way to do this is to copy each row containing Todo into the register, and then paste the contents of the register into another file, and you can save it all the time.
We ll use the A register. First we ' ll need to clear it by runningqaq
. Let's break this down: qa
Tells Vim to start recording a macro into the a register, and thenq
Stops the recording. We didn ' t type anything while the macro is recording, so the register ends up empty. We can check this by running the following:
We want to use registers, the first thing is to empty the contents of the register. You can use the commandqaq
Realize. Keep this order separate.:qa
Tell Vim to start recording macros to registers,q
Tell Vim to stop recording. We did not enter any characters during the recording, so the register was eventually empty. We can use the following command to check.
:rega?---Registers---"a
Now we can go ahead and yank the TODO comments into the register:
Next we go ahead and copy the TODO content to the register:
:g/TODO/yankA:reg"a // TODO: Cache this regexp for certain depths.// TODO: No matching end code found - warn!
The
The trick is, we ' ve addressed our register with an uppercase A
. That's 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 matches the pattern /todo/, append the entire line into register a. "
The clever thing here is that we use uppercase when we reference our registers. • A
, which tells Vim to add content to a particular register, and lowercase a
to overwrite the contents of the register. We can interpret this command as "adding a row containing todo to register a"
This time, if we run : Reg A
, we can see that the register contains T He and TODO items from the document. (For the sake of legibility, I ' ve formatted these items on both separate lines, but in Vim it actually shows a ^j symbol F or newlines.) We could then open up a new buffer in a split window and run "APs
to paste the a register into the new documen T.
We execute the command this time : Reg A
, we can see that the register contains two TODO items in the document. We can open a new buffer in a new window and then execute the AP
command to paste the contents of the register into the new document.
[Practical.vim (2012.9)]. Drew.Neil.Tip99 Learning Summary