In his (very good) book "Pratical Vim" Drew Neil shows how to collect all the lines containing the word "TODO" into a register to use them latter.
To do so he simply use a global command: :g/TODO/yank A
(The capital A
allows to happen lines to the named register a
).
I think that's a pretty cool trick but I need to improve it: I'm trying to insert the number of the line before it's content. I think the solution would be to get the line number and use it in the last part of the command I mentioned before. The problem is that I don't know how to get this line number.
So my question is: How in a global command can I get the number of the matched line and how can I use this number?
Just to be clear here is an example. Let's consider this file:
1 //TODO: Hey this is a todo
2 int main(void){
3 //TODO: and this is another one
4 printf("Hello world");
5
6 return 0;
7 }
When I type :g/TODO/yank A
and I put the content of the register in a file I get:
//TODO: Hey this is a todo
//TODO: and this is another one
What I would like to get is:
1 //TODO: Hey this is a todo
3 //TODO: and this is another one
Bonus the yank also include the indentation of the line, it would be pretty cool if I could remove it directly from the global command.