Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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

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

6 Answers 6

up vote 38 down vote accepted

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


does it for me.

share|improve this answer
Nice! Is there an equivalent for x? When I try :4x I get E140: Use ! to write partial buffer. Thanks! –  dotancohen Jun 28 '12 at 17:01
I have to say this is amazing :D –  nXqd Aug 8 '12 at 7:11
@dotancohen :4d – Oct 7 '13 at 0:30

Try this:

share|improve this answer

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


You can apparently also do it with a pattern:


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

share|improve this answer
Even shorter (t as a copy command is a left-over from ed): :4t. –  tarleb 3 hours ago



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.

share|improve this answer

This should do it:

share|improve this answer
No, that yanks line numbers 4 to 12. –  Lazer Aug 26 '10 at 16:00
I meant copy and paste line 4, to line 12. –  Lazer Aug 26 '10 at 16:01
Edited. Is this what you wanted? –  Umang Aug 26 '10 at 16:10

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.)

share|improve this answer

Your Answer


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.