How can I search and copy strings in vi / vim?

I think it’s better to clarify 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 looking for the string "useful." So, I want to copy lines 1, 4 and 5 to the clipboard. How can I do this with vim?

+4
source share
1 answer

The first explicit register is a (you can use any letter az) to use.

 :let @a='' 

Then run the magic.

 :g/useful/yank A 

He will look for lines matching the useful pattern, and then run the command :yank A Capital A will be added to register a .

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

 :let @ +=@a 

to copy the contents of register a to the clipboard.

+8
source

All Articles