Vim Tip: Copy & Paste Search Results
This is a quick tip to capture search results in Vim to a register, and paste them into another document.
The sample text
Lemon cake
Chocolate cake
Chocolate chip cookies
Oatmeal cookies
German chocolate cake
Mamorkuchen
Peanut butter cookies
I want to copy all the lines containing chocolate, and paste them into a new document. Here is the sequence of commands:
:redir @a --start capturing in register a
:g/chocolate --execute search
:redir END --close register a
:new --create a new document
:put! a --paste contents of register a into the new document
Thats it. Now I can finally recycle this little cheatsheet I created back in 2011!
Bonus tip: Count occurrences of a pattern in Vim:
:%s/chocolate//n