Search code examples
searchvimpasteyank

how to copy and paste a vim search result to a new tab


I trying to search and copy the matches within a limited scope in vim. as mentioned in this answer. I am trying to copy the result into a new vim tab using

:'<,'>g/foo/y A

but when a open a new tab :tabnew and try to paste p nothing is showing.
1. so, my question is if i can redirect the gsearch output into a new tab?
2. or how do i properly copy the output into a register and paste from the register to a new tab?


Solution

  • Solution for option 1:

    Redirecting matched text to new tab

      :tabnew | :normal! i^R/
    

    To put ^R, you have to press Ctrl and V, and then R.

    The above command pastes the contents of search register, which is foo in your case. If you wanted to put the content of any other register, you can put that character instead of slash.

      :tabnew | :normal! i^Ra
    

    This will paste the contents of register a, in which you have saved the output of global search command.

    Solution for option 2: properly copy and paste through register

    Your command works perfectly! It copies the output of global search to register a. So, you can directly open another tab by :tabnew and then press "ap to paste it, or go to insert mode by pressing i and then press Ctrl and R, and then a. The contents of register a is pasted there.