50

For example

alt text

How can I yank and paste Line 4 only to Line 12 without having to move the cursor to Line 4?

alt text

2
  • I answered your question base on the fact that "yank me!" is on line 4. In your 2nd screenshot, it appears on line 5. Aug 26, 2010 at 16:29
  • Thanks @Louis for the answer. I have updated the screenshot.
    – Lazer
    Aug 26, 2010 at 18:23

9 Answers 9

64

If the cursor is already on line 12, then a simple

:4y
P

does it for me.

6
  • 1
    Nice! Is there an equivalent for x? When I try :4x I get E140: Use ! to write partial buffer. Thanks!
    – dotancohen
    Jun 28, 2012 at 17:01
  • 1
    I have to say this is amazing :D Aug 8, 2012 at 7:11
  • 1
    @dotancohen :4d
    – n.st
    Oct 7, 2013 at 0:30
  • 1
    Is there a good way to do this with relative line number?
    – TrungDQ
    Aug 21, 2016 at 15:47
  • 1
    With relative numbers just use + and -. So -4 is four rows above the cursor and +4 is four rows below it. Oct 9, 2017 at 15:14
12

How about this: Cursor is on line 11, you're in "vi" mode.

:4co.<return>

You can apparently also do it with a pattern:

:/^Yank/co.<return>

You could use "mo" (move) instead of "co" (copy) to just move the line, instead of yank and put.

2
  • 3
    Even shorter (t as a copy command is a left-over from ed): :4t.
    – tarleb
    Oct 4, 2015 at 20:02
  • 1
    Similar, but with patterns is to search for a pattern above and paste where the cursor is without moving, say, with :?pattern?t. To search for a pattern below just turn the ? into /
    – Joe
    Apr 4, 2021 at 3:14
9

Try this:

:4,4y  
P
8

Try:

:4co11

You can use an argument of 0 to paste to line 1. This will also work with ranges:

:m,n co k

will copy lines m through n to line k+1. In addition it doesn't matter where you are in the buffer. The move command, m, works similarly.

4

Others have already mentioned the most direct method of doing this, which is :4y, but I want to add two notes about it which may be useful:

One, if you type in :help range you will learn all about using ranges to apply to Ex style commands. VERY useful in many cases, and far more versatile than using line numbers. For instance, if you want to yank the first line starting with "yank" after the start of the document (even if it's on the first line), use :0/^yank/y. The most powerful part of ranges is the ability to combine them, e.g., :./^Chapter/+50?Pete?d means "Find the next line to start with the word Chapter (searching forward from the current line), then jump forward 50 lines, and search backward for the word Pete, and delete the first line you find it in." Except that it doesn't actually move the cursor, it just DOES what you tell it.

(I've used this type of range specification to automate fiddly documentation creation involving searching for the function name under the cursor, then searching forward for the next line which said "Description" and yanking it to a specific register...etc.)

The other tip, is that you should know about jumping. Type :help jump-motions to learn about it fully, but the way it relates here is that you can jump to line 4, yank it and then RETURN with 4Gyy'' (or use backticks instead of apostrophes to jump back to your exact starting position, rather than just the same LINE you started at.)

3

Shortest:

:1t. copies the first line below the current.

With ranges: :1,4t. copies line 1 to 4 (inclusive) below the current.

Also possible with relative line numbers:

:-1t. copies the previous line below the current.

In your example :4t-1 would copy the fourth line below the wanted line.

Note: :t is a synonym for the :co[py] command.

2

This should do it:

:4,12y
p
k
3
  • 1
    No, that yanks line numbers 4 to 12.
    – Lazer
    Aug 26, 2010 at 16:00
  • I meant copy and paste line 4, to line 12.
    – Lazer
    Aug 26, 2010 at 16:01
  • Edited. Is this what you wanted?
    – Umang
    Aug 26, 2010 at 16:10
1

I used the :co. solution from this thread and created remaps for them, as I use them quite regularly for relative copying; this remap is suited for a qwertz keyboard:

nnoremap - :-co.<left><left><left>
nnoremap _ :+co.<left><left><left>

This way, only the relative line number has to be entered.

0

Building on Erwin Rooijakkers's answer, this copies line 4 below line 11, regardless of where your cursor is:

:4t11

You must log in to answer this question.

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