sponsor Vim development Vim logo Vim Book Ad

basic Tip #478: Copy the search results into clipboard

 tip karma   Rating 18/10, Viewed by 2019 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   May 22, 2003 7:18      complexity:   basic
author:   JAS      as of Vim:   6.0

" previous clear the clipboard with this command :normal "*y0
" Usage: :g/<pattern>/call CopyPattern()
function CopyPattern()
let idx = 0
let xEnd = 0

while idx >= 0
let @* = @* . matchstr(getline("."), '' . histget("/", -1), idx) . "\n"
let xEnd = matchend(getline("."), '' . histget("/", -1), idx)
let idx = match(getline("."), '' . histget("/", -1), xEnd)
endwhile

unlet idx
unlet xEnd
endfunction

 rate this tip  Life Changing Helpful Unfulfilling 

<< How to put the indentation level on the status line | Replace with NO Typing >>

Additional Notes

[email protected], June 13, 2003 7:55
a few more details or an example would be helpful, do you run this command in your vimrc file and what is it doing exactly?  I was looking to create a mapping to easily copy and paste text from my windows session to my vim using something like vmap <C-c> "*y but this did not seem to work in non gui mode, and I was lookign for some answers with that... this might be somethign different but I still could not understand the context of this example immediately from reading it.  thanks, ben
[email protected], July 26, 2006 23:01
The following one is for multi-line pattern (like using  \_.).

" previous clear the clipboard with this command :normal "*y0
" Usage: :g/<pattern>/call CopyMPattern()
function CopyMPattern()
let resultTxt = ''
let allLines = getline(".", "$")

for line in allLines
  let resultTxt .= line . "\n"
endfor

echo resultTxt

let @* = @* . matchstr(resultTxt, '' . histget("/", -1), 0) . "\n"

unlet resultTxt
unlet allLines
endfunction
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo