51

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?

pfrenssen
  • 613
  • 1
  • 5
  • 5

5 Answers5

71

From :help :yank:

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

So, to yank line 4, one would type:

:4yank

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:

<C-o>:4yank

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.

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
43

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:

:4t.

where…

  • 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:

:4t-

where…

  • 4 is still the line to copy,
  • - is the line above the current line.
romainl
  • 40,486
  • 5
  • 85
  • 117
3

Simply type

:4y

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
jamessan
  • 11,045
  • 2
  • 38
  • 53
  • 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 it will yank til the 4th following occurrance of "foo", etc. – Don Hatch May 13 '15 at 05:34
  • The last command should read 4G"ayy. – romainl May 13 '15 at 08:13
1

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)
Stryker
  • 173
  • 7
1

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

Janis
  • 278
  • 2
  • 7
  • 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., http://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