3

I was reading through the vim wiki about the power of g https://vim.fandom.com/wiki/Power_of_g and would like to do something similar to :g/pattern/y A but a little bit different.

I have an html file with tags that have a bunch of properties like this:

<the-tag
  prop="prop1"
</the-tag>
<the-tag
  prop="prop2"
</the-tag>

I would like to append to register a, the value inside the prop quotes. Is this possible using :g or some other method? Here is what I tried:

:g/prop/normal yi" A

but this just yanks the very last prop value instead of a whole list. Of course I could do:

:g/prop/y A

but this would give me:

prop="prop1"
prop="prop2"

which is not what I'm looking for. Is this possible?

5

You're confusing the passing of the register to :yank (Ex command) vs. y (normal mode). For the latter, instead of appending the register, it must come before the y command, and with the " prefix: "Ayi" (with register A, yank the inner " double quoted text)

:let @a = '' | g/prop/normal! "Ayi"

Note that as the yank is characterwise, all matches will be appended without any separator (resulting in prop1prop2). You could add a separator like this:

:let @a = '' | :g/prop/let @a .= " " | normal! "Ayi"
| improve this answer | |
  • Thanks! This was really helpful. I'm a little confused about your second example though. I did notice the characterwise issue with the first command and the second one did fix it, but I'm not sure I understand whats going on with the \d/ part of it. I can see you are appending to a with let @a .= " " but how does the \d factor in? – instantaphex Nov 27 '19 at 19:41
  • Oh, I just added the \d to make it match just once in a line. Doesn't matter with :global, though; just ignore it. – Ingo Karkat Nov 27 '19 at 20:17
1

Somebody had a similar problem: vim yank all matches of regex group into register.

In your case regex will be prop="\zs.*\ze".

\zs denotes where the match will start in the "substitution" (yanking to register), and \ze where to end.

| improve this answer | |
0

A slightly different variation using :h gn motion:

let @a='' | g/prop="\zs.*\ze"/normal! "Aygn
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.