4

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 !!) ?

TCSGrad
  • 11,898
  • 14
  • 49
  • 70

1 Answers1

15

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

Community
  • 1
  • 1
Benoit
  • 76,634
  • 23
  • 210
  • 236
  • Great answer - but I want to understand it a bit more, for instance: :g/pattern/y A => Why in append mode (is it to save previous value in register a), and secondly, is 'a' the default register ? – TCSGrad Jul 25 '11 at 19:59
  • 3
    The default register is `"` which sadly doesn't have an uppercase, so it can not be used this way to append. The reason for appending is so you get all the matching lines not just the last one. Imagine that you yank a line, then move to another line and yank that one. When you finally did a put you would have just put the last line. Appending will allow you to capture multiple lines. – Peter Rincker Jul 25 '11 at 20:20
  • 2
    Clearing register `a` if you are in normal mode can be done using `qaq` (starting recording a macro and stopping before pressing anything else) which is faster. – ZyX Jul 26 '11 at 03:13
  • @ZyX: nice way to do it also, thank you! Seems obfuscated though :) – Benoit Jul 26 '11 at 05:42