Combining search movement with yank
y/
or y?
to yank
till a specific search hit is a productivity boost that I rely on frequently but there's something that's been bothering me for a while and my searches in docs and the web haven't yielded an answer thus far: how can you choose a different hit in the middle of the yank search movement? eg say I have a buffer with the following contents:
1: foo bar
2: foo baz
3: This line contains the cursor.
Say I type y?foo
in normal mode and realise that I don't want the foo in line 2 but its occurrence in line 1开发者_C百科; is there a key I can press at this point to navigate between the different hits before choosing the one I need to complete the yank operation? (n and N don't apply as they change the search pattern)
This simple example does not do justice to the usual large buffer content I deal with and so I might not be able to see the search hits beforehand. Also, at times I could make a mistake and realise that I want to find another occurrence in the middle of the operation. Basically, I want to be able to lazily change course during the operation.
Since this is interactive, use visual mode:
v?foo<cr>ny
Given your sample buffer above, this will search backwards for "foo", find another (backwards) match, and then yank all of the (visually-)selected text.
Visual-line mode is uppercase V and also often helpful. Visual-block mode is ctrl-V and sometimes helpful.
Mark your spot, search, and yank up to the mark:
ma
?foo
n
y`a
Well you could modify your regex so it only matches before a certain line:
y?foo\%<2l
I think it's just easier to use visual mode to make sure you are yanking exactly the right text:
v?foo<CR>y
精彩评论