Friday, July 6, 2012

Re: yank matched portion of a selection of lines

On Friday, July 6, 2012 10:12:07 AM UTC-5, Hoss wrote:
> I have a buffer open. I hit /, put in a search, and the matched text, which is the text I am interested in, is highlighted.
>
> How do I yank that text, and only that text, in other words, not whole lines? Bonus points if I can also restrict it to a selection of lines, by number.

Do you want to yank a single match or all matches within the range?

For a single match, after searching to put the cursor where you want it, type y//e and hit enter. For multiple matches I'd probably do something like this, but there's probably a better/cleaner way:

qaq <- quick way to clear "a" register
:123,456g#pattern#exec "norm! /pattern\<CR>\"Ay//e\<CR>"

This will only work if there's no more than 1 patch per line, here's the breakdown:

123,456g#pattern# <- operate on all lines between 123 and 456 (inclusive) which include a match for "pattern"
exec "norm! ..." <- do a normal-mode command on each line, as follows:
/pattern\<CR> <- search for the pattern
\"Ay <- yank from the cursor position and append into "a" register
//e\<CR> <- tells where to yank to, in this case to the end of the match

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: