Vim Tips Wiki
Juyeone (talk | contribs)
(adding comment with links to tips that may be a better solution for some uses of this tip)
Line 76: Line 76:
 
endf
 
endf
 
</pre>
 
</pre>
  +
  +
----
  +
  +
Depending on the reason you want your search results in the clipboard, there may be better ways to accomplish this.
  +
  +
If you want to view a window of search results, and are using this method to copy and then paste them into a new window, see [[Find in files within Vim]]. You can use '%' for the search path to search only the current file.
  +
  +
If you want to view only the parts of a file that match some regular expression, see [[Folding with Regular Expression]]. This will allow you to use 'zr' to view some context around your search as well.
  +
  +
If you actually need the search results in the clipboard for some reason, like pasting it into an external application, this method is probably a good one (though I haven't tried it).

Revision as of 17:15, 4 February 2008

Tip 478 Printable Monobook Previous Next

created May 22, 2003 · complexity basic · author JAS · version 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

Comments

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


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

I think this function works well. Please, try on it~

fu! CopyMPattern()
  let posinit = getpos(".")
  cal cursor(1, 1)
  let snum = 0
  let enum = 0
  let cnt = 0
  let searchedLines = []
  wh 1
    let snum = search(histget("/", -1), '', line("$"))
    if snum == 0 | brea | en
    let enum = search(histget("/", -1), 'e', line("$"))
    let searchedLines = searchedLines + getline(snum, enum)
    let cnt += 1
  endw
  let resultTxt = ''
  for line in searchedLines
    let resultTxt .= line . "\n"
  endfor
  let @+ = resultTxt
  cal cursor(posinit[1], posinit[2])
  echom cnt 'lines(or blocks) were copied to the clipboard.'
endf

Depending on the reason you want your search results in the clipboard, there may be better ways to accomplish this.

If you want to view a window of search results, and are using this method to copy and then paste them into a new window, see Find in files within Vim. You can use '%' for the search path to search only the current file.

If you want to view only the parts of a file that match some regular expression, see Folding with Regular Expression. This will allow you to use 'zr' to view some context around your search as well.

If you actually need the search results in the clipboard for some reason, like pasting it into an external application, this method is probably a good one (though I haven't tried it).