21

A lot of times I'm in a situation similar to this:

1. <Line 1>
2. <Line 2>
3. <Line 3>
4. <Cursor is here>
5. <I want to copy line 1 here>

An easy solution is mm:1<CR>yy`mp - that is, make a mark at line 4, go to line 1, copy it, go back to the mark, and paste.

Is there a shorter/more fluid solution to this? I do this often enough that I'm happy to make a mapping for it.

2 Answers2

23

Looks like a case for ex copy (:help :co or :help :t):

:1t4

or, using your cursor's position:

:1t.

This does not copy the line to the yank register, which might or might not be what you want.

PhilippFrank
  • 1,225
  • 9
  • 13
11

You could shorten it by not using marks and yank the line directly.

:1y<CR>p

Command version of yank takes a {range} so select a line or a group of lines.

Additionally, the range value can be either absolute or relative. Lines above the cursor are negative distance and below are positive. So yanking two lines above is :-2y and yanking two lines below is :+2y.

jecxjo
  • 2,660
  • 13
  • 24