Say that I am on line 20 and I would like to yank line 4, how can I do that?

And similarly, how can I yank a line relative to my cursor position, say the one 3 lines up?


From :help :yank:

:[range]y[ank] [x]      Yank `[range]` lines [into register x].

So, to yank line 4, one would type:


Note you can easily do this from insert mode with <C-o>; this allows you to execute one command, after which you're returned to insert mode; for example:


You can, of course, also use other ranges. Some examples:

  • Lines 1 to 3: :1,3yank
  • The entire buffer: :%yank
  • From the current line to the end of the buffer: :.,$:yank
  • The current line and the next 3: :.,+3yank
  • The current line and the previous 3: :-3,.yank
  • The line 3 lines above the current line: :-3yank

The most useful things to remember about ranges:

  • It's in the form of :line1,line2command.
  • A . is the current line (you can actually omit the dot in most cases; :.,+3yank and :,+3yank are the same)
  • You specify lines relative to the current position with +n and -n.

See :help [range] for more information.

| improve this answer | |
  • 6
    Bonus points for mentioning <C-o>, I'm underusing that. – PhilippFrank May 12 '15 at 11:37
  • 7
    It's also worth mentioning that you can abbreviate :4yank with :4y – NewbieOnRails May 12 '15 at 18:44

In addition to Carpetsmoker's answer, I should point out the awesome :help :m and :help :t.

If you want to copy line 4 to right below the current line you can do this:


or that, if you want to copy that line right above the current line:

| improve this answer | |

Besides the ex-mode commands that you've got you can achieve that also it in command mode, e.g. by: 4GY'' - which means: goto line 4 (4G), yank line (Y), and return to previous line ('').

You can also use jump marks; for your second question, e.g. by: mm3kY'm - which means: set mark m (mm), go three lines up (3k), yank line (Y), return to mark m ('m).

| improve this answer | |
  • 1
    Using `` is probably a better recommendation than '' since the former goes back to the same line and column. Ditto for `m vs. 'm. – jamessan Jun 15 '15 at 14:08
  • I agree. (Any idea how to create literal backtics in wiki markup since they have a special meaning?) - Otherwise your comment will serve well as amendment to my answer and as hint for the readers. – Janis Jun 15 '15 at 15:58
  • You have to use multiple backticks to delimit the inline code (c.f., daringfireball.net/projects/markdown/syntax#code). I know it works in questions/answers, but I couldn't get it to work in my comment. – jamessan Jun 15 '15 at 16:10

Simply type


to yank line 4

it will go into the unnamed register. Then, (for example) you can use p to put it elsewhere. You can also use [n]p, e.g. 10p to paste it 10 times.

You can put it into a named register such as "a" with

:4y a
| improve this answer | |
  • 2
    The part about putting it into a named register with "ay4 doesn't look right. If you do that, vim will be waiting for you to say "4 whats?". E.g. if you then hit Enter it will yank 4 lines starting at the current position, if you type /foo<Enter> it will yank til the 4th following occurrance of "foo", etc. – Don Hatch May 13 '15 at 5:34
  • The last command should read 4G"ayy. – romainl May 13 '15 at 8:13

You can do

:10y <Enter> (to copy the line 10)
p (paste line 10 where the cursor is)

Setting up relative number helps in moving between lines of code as well.

:set relativenumber

Now you can copy the 5th line above the cursor with:

:-5y <Enter>
:p (to paste)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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