Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

let's say I have a regex inside a line and I want to copy the first line above the cursor position that has that pattern without moving the cursor. For example (cursor being marked with ||):

This is a short example.  
On the other hand,  
I'm having trouble with it. ||

How to copy the sentence "This is a short example" to the position of the cursor (II)? If I search for the sentence, the cursor will be moved to This (?This is).

share|improve this question

2 Answers 2

up vote 3 down vote accepted

You could do:

:?short?t.

It does a backward search for short (?short) and copies the matching line (t) to after the current line (.).

You can read about Ex commands in :h :index and about ranges in :h range.

edit

In reality, it doesn't exactly work like that but I think the explanation above may make a little more sense.

The exact mechanism is succinctly explained in :h :copy:

:[range]co[py] {address}
    Copy the lines given by [range] to below the line given by {address}.

Here, the [range] is the first line above the current one that matches short and the {address} is the current line, ..

I love Vim.

share|improve this answer
    
@romaini wow, thanks! :) –  tomislav-maric Jan 21 '13 at 13:32
    
@romaini :) vim IS the best editor. ever. –  tomislav-maric Jan 21 '13 at 14:53

There are a couple of ways and plugins that come to my mind, but the natural way would be to just search (?This is), yank, and then jump back to the original position with Ctrl + O and paste. With the help of the jump list (cp. :help jumplist), the temporary move isn't so bad.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.