How can I search for and then copy the lines in vi/vim?
clipboard, vi, vim
Solution
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.
Problem
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?