Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I search for and then copy the lines in vi/vim?

Tags:

vim

vi

clipboard

I guess it is better to explain with an example, so here it is:

My useful line.
Some useless line.
Some other useless line.
Another useful line - oh I want this!
This is useful, I want this too!

In this example, I am searching for the string "useful". So, I want to copy lines 1, 4 and 5 to clipboard. How can I do this with vim?

like image 952
crnlx Avatar asked Feb 19 '23 10:02

crnlx


1 Answers

First clear register a (you can use any letter a-z) for using.

:let @a=''

Then run the magic.

:g/useful/yank A

It will search for lines matching pattern "useful" and then run command :yank A to them. Capital A will append to the register a.

If your vim is configured with global Windows/X clipboards, you can run

:let @+=@a

to copy the register a's content to the clipboard.

like image 79
mike3996 Avatar answered Mar 05 '23 21:03

mike3996