Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select all lines maching a pattern?

Tags:

vim

Is it possible to select all lines matching a pattern?

To illustrate, using column :

      1     2  3                                   1 2 3
   2     1  4      select all lines matching 1     2 1 4
1 2      2            :'<,'>!column -t             1 2 2
  3   2    3            ->                           3   2    3
 1 1   3                                           1 1 3
    4 4     4                                        4 4     4
like image 619
odessos Avatar asked Mar 01 '26 03:03

odessos


2 Answers

If I well understood your question, the answer is quite simple:

qregisterq
:g/pattern/y REGISTER

The first line is necessary to clean up the register's content, if you used it before. In the second line it's important to capitalize the register's name because that means that you intend to append the content of each matching line, instead to substitute it.

To paste the matching lines simply do:

"registerp

or even more simple:

p

because p copy the content of the last register used.

For example, to select all lines matching 1 (using register a):

qaq
:g/1/y A

and then:

"ap
like image 99
aglien Avatar answered Mar 02 '26 21:03

aglien


Vim doesn't have the ability to match multiple separated lines by default.

You could look at this script, or this one (more up to date) and see if it works for you.

However, if you are just selecting the lines to perform a command on them, you can use the :g command to perform the command directly on those lines.

:g/regex/ex command

So for example:

:g/hello/d

Will delete all lines that match the regex hello

With the multiple cursors plugin (also linked above) you can select multiple separated lines that match a regex:

:% MultipleCursorsFind Regex

This will select the lines for you and then you can execute any commands you like on them.

like image 45
Zach Avatar answered Mar 02 '26 20:03

Zach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!