Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining search movement with yank

Tags:

vim

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; 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.

like image 748
argetek Avatar asked Feb 16 '11 00:02

argetek


2 Answers

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.

like image 75
Fred Nurk Avatar answered Oct 04 '22 15:10

Fred Nurk


Mark your spot, search, and yank up to the mark:

ma
?foo
n
y`a
like image 35
wilhelmtell Avatar answered Oct 04 '22 14:10

wilhelmtell