Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 3 down vote accepted

You could do:


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.


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! :) – tmaric Jan 21 '13 at 13:32
@romaini :) vim IS the best editor. ever. – tmaric 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .