Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

vim

I want to search within a function, and copy all lines which are calling a different function within that scope, to a different file. I know I can limit the scope of the search by visual selection, and search is easy - but I'm not getting a convenient way of copying all search results to any buffer (which I can then paste to another file for my analysis)... Can someone point to the solution (I'm almost sure it would be pretty easy, but for some reason, its not obvious to me !!) ?

like image 333
TCSGrad Avatar asked Dec 06 '22 20:12

TCSGrad


1 Answers

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

Maybe this answer about registers can be of some help for you.

An alternative if you want to APPEND to your other buffer:

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

References : :help :g, :help :#, :help :redir

like image 151
Benoit Avatar answered Dec 09 '22 10:12

Benoit