Sam Giles
Sam Giles

Reputation: 650

How to visually select lines based on a search pattern in vim?

I've got some method calls all over the place in a large file, I'd like to match these lines, select and then yank them as one so I can put them all in a single place.

I can find all the lines I want with :g/>set but how do I visually select each line?

Upvotes: 6

Views: 2648

Answers (2)

romainl
romainl

Reputation: 196886

You can't have multiple visual selections in Vim.

But you can clear a register and append all the matching lines to it:

:let @a = ''
:g/>set/y A

then create an empty buffer (or navigate to an existing one):

:vnew

and paste from register a:

"ap

But you probably want something like TagList or TagBar.

edit

:[something]y a

means "yank into register a".

:[something]y A

means "append to register a".

Upvotes: 7

Xavier T.
Xavier T.

Reputation: 42278

What I usually do is :

  1. Remove all the lines without the pattern :v/pattern/d
  2. Select the whole new file with ggyG
  3. Paste somewhere the result with p
  4. Use undo a few times with u to get the file back to its initial state

This is a bit cumbersome, I would welcome a simpler solution.

Upvotes: 3

Related Questions