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?


5 Answers 5


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.

By default the lines will be yanked into the default register (""). If you want to use a named register, add a space an then the name of the register (naked, not prefixed with a double quote), eg:

:30,30yank a
> 10 lines yanked into "a

See :help [range] for more information.

  • 7
    Bonus points for mentioning <C-o>, I'm underusing that. May 12, 2015 at 11:37
  • 10
    It's also worth mentioning that you can abbreviate :4yank with :4y May 12, 2015 at 18:44

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

The canonical behavior of :<range>t<address> and :<range>m<address> is to copy/move the lines in <range> below the given <address>.

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



  • 4 is the line you want to copy,
  • . is the current line.

See :help :range.

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



  • 4 is still the line to copy,
  • - is the line above the current line.

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
  • 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, 2015 at 5:34
  • The last command should read 4G"ayy.
    – romainl
    May 13, 2015 at 8:13

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

  • 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, 2015 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, 2015 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, 2015 at 16:10

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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