20

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.

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.

| improve this answer | |
  • I do not use ex copy enough, good suggestion. – jecxjo Apr 24 '15 at 14:25
10

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.

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