How do I yank all matching lines into one buffer?

后端 未结 3 1168
情话喂你
情话喂你 2021-01-30 06:26

How do you yank all matching lines into a buffer?

Given a file like:

match 1
skip
skip
match 2
match 3
skip

I want to be able issue a c

相关标签:
3条回答
  • 2021-01-30 07:02

    :g/^match/yank A

    This runs the global command to yank any line that matches ^match and put it in register a. Because a is uppercase, instead of just setting the register to the value, it will append to it. Since the global command run the command against all matching lines, as a result you will get all lines appended to each other.

    What this means is that you probably want to reset the register to an empty string before starting: :let @a="".

    And naturally, you can use the same with any named register.

    0 讨论(0)
  • 2021-01-30 07:15
    :help registers
    :help quote_alpha
    

    Specify a capital letter as the register name in order to append to it, like :yank A.

    0 讨论(0)
  • 2021-01-30 07:18

    Oh I just realized after commenting above that it's easy to yank matching lines into a temporary buffer...

    :r !grep "pattern" file.txt

    The simplest solutions come once you've given up on finding them. :)

    0 讨论(0)
提交回复
热议问题