1

How is it possible to :hardcopy all the lines - and only those - resulting from a pattern search ?

I tried :

:g/pattern/ha

but it did not work (3 occurrences gave 3 hardcopies of the entire file...)

Thanks in advance

PS : I should have added that I wish to print the line numbers AND keep the original line numbers (e.g, pattern in lines 3, 7, 8, print 3, 7, 8 and not 1, 2, 3)

ThG
  • 2,361
  • 4
  • 22
  • 33

1 Answers1

3

Try deleting those lines which do not match.

:v/pattern/d
:ha

To capture the output of :g//nu, look into the :redir command, according to Capture ex command output on the Vim wiki. They give this example:

:redir @a
:g//nu
:redir END

and then paste the output into a new buffer, which you can print.

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • It works, except that it does not keep the original line numbers (e.g. pattern in line 3, 7, 8) and outputs the pattern with line numbers 1, 2, 3 – ThG May 12 '11 at 14:28
  • I've added a bit about `:redir` (a command with which I was unfamiliar); see http://stackoverflow.com/questions/2573021/vim-how-to-redirect-ex-command-output-into-current-buffer-or-file – Josh Lee May 12 '11 at 14:54