2

This answer teaches me how to yank matches of regex group into one register. But I want to yank two into two different registers. I've tried the following but failed.

:%s/\(regex1\)\(regex2\)/\=setreg('a', submatch(1))\=setreg('b', submatch(2))/n

An error thrown out says invalid expression. Then I modified it to be like this:

:%s/\(regex1\)\(regex2\)/\=setreg('a', submatch(1)) | \=setreg('b', submatch(2))/n

Then I get the error E493 backwards range given.

Can I do what I want? If so, how should I correct this snippet?

1 Answer 1

5

The idea is to give back a single (replace) result; so, concatenate works:

:%s/\(regex1\)\(regex2\)/\=setreg('a', submatch(1)) . setreg('b', submatch(2))/n
2
  • Yeh, it works. Thanks. Can you tell me how to get the help manual for me to further learn it myself? Aug 16, 2015 at 9:56
  • 1
    You're welcome - I went from reading :help :substitute to :help sub-replace-special, and from there, to :help sub-replace-expression, as this is were the reading of those help sections pointed me to (by following \=). And in the 3rd mentioned section, there is an example that concatenates 3 strings in the replace part. This gave me the idea.
    – VanLaser
    Aug 16, 2015 at 10:03

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .