tags:

views:

76

answers:

3

If I have the following words

cat Oliver Louis

and the cursor is on "c", I can copy up to the beginning of Louis with y/Louis<CR> (where "CR" is the enter key), which will copy the string "cat Oliver ".

Is there a way to copy the search term, "Louis", as well, for a copied string of "cat Oliver Louis"? Thanks in advance.

+1  A: 

One way is to use look-behind assertions... but the syntax is rather ugly!

y/\(Louis\)\@<=

This doesn't look for Louis, but for a position preceded by the string Louis.

UncleZeiv
Unfortunately, I am looking for something convenient; I think it might be easier to go into visual mode, search for the term and hit "w" or the like -- it's less mental overhead. Thanks anyway though.
gatoatigrado
With `\zs`, it would have been easier -> `y/Louis\zs`.
Luc Hermitte
@Luc, wow thanks, didn't know about that!
UncleZeiv
+2  A: 

Since the / search is exclusive, it will select everything up to the match, but not including the match. (check :help exclusive)

A more convenient way might be to use the inclusive f and t motion commands.

With the following line:

cat Oliver Louis

typing yfs in normal mode will yank everything up to the letter "s", or in this case, cat Oliver Louis

Obviously, this isn't as convenient if the line is something like

cat Assassins Scissors

There are too many s's. In this case you might want to go into visual mode and either repeat the inclusive f search motion by tapping ; until you reach the last s. Or, as you suggested above, simply use w preceded by the number of words you'd like to copy. In this case y3w will work.

michaelmichael
didn't know about ;, thanks for that :)
Matt Briggs
+9  A: 

Use the /e modifier: y/Louis/e<CR>. If you want to repeat the last search pattern, just leave it out: y//e<CR>.

:help search-offset
too much php