VIM: How to copy all lines with a substring to another file / buffer?

I want to search inside a function and copy all the lines that call another function inside this area to another file. I know that I can limit the search to visual selection, and the search is simple, but I don’t get a convenient way to copy all the search results to any buffer (which I can then paste into another file for my analysis) ... Can someone point to solution (I'm pretty sure it will be pretty easy, but for some reason this is not obvious to me !!)?

+4
source share
1 answer

You can use:

:let @a='' " clear register a. A faster alternative would be "qaq" in normal mode (thanks ZyX) :g/pattern/y A " yank all lines with pattern into register a, but in append mode (hence a capital letter) :buffer other_file.txt "ap 

Perhaps this answer about registers might help you.

Alternative if you want to add APPEND to another buffer:

 :redir >> other_file.txt :g/pattern/# "alternative: :g/pattern/print or :g/pattern/number. :redir END 

Links:: :help :g :help :redir :help :# :help :redir

+14
source

All Articles