Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 585
Sam Giles Avatar asked Sep 06 '13 14:09

Sam Giles


2 Answers

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".

like image 200
romainl Avatar answered Oct 01 '22 12:10

romainl


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.

like image 23
Xavier T. Avatar answered Oct 01 '22 11:10

Xavier T.