Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick yanking in Vim

I often find myself in a situation like this:

line i want to yank
.
.
.
[cursor position]

I want to yank the line and paste to [cursor position].

I'm doing that with [n]k, Y, [n]j, p. That's a lot of typing ;). Before, I was using:

:[line number]Y

and then pasting, but that doesn't work with relative line numbers (relativenumber option).

What's the fastest way to do such yanking/pasting while relative numbers are ON?

like image 717
pbp Avatar asked Mar 02 '12 13:03

pbp


4 Answers

If the line is within sight (before or after cursor position), I normally just search for it (?line i want), yank, then go `` (back to previous position), then p.

like image 94
Manish Avatar answered Nov 08 '22 01:11

Manish


I'll try this:

  • ?yank+Enter <== type significant patten to search backward
  • yy
  • Ctrl+O <== go back
  • p
like image 24
kev Avatar answered Nov 08 '22 00:11

kev


 :/your desired search for the specific line/ y

or

 :N y

(where N is the specific line number) will copy the specific line (and it can be a range too). Now you hit p to paste it. This way you does not move the cursor.

like image 5
Zsolt Botykai Avatar answered Nov 08 '22 01:11

Zsolt Botykai


When the number of the line to copy is known (whether it is absolute or relative), a convenient way to duplicate that line is to use the :copy command. For example, the Ex command below copies the line which is four lines above and pastes it below the current one.

:-4t.
like image 4
ib. Avatar answered Nov 08 '22 01:11

ib.