1

How to remove a line and paste it at the beginning of a range?

line 1
line 2
line 3
line 4
xxxxxxxx

For example, here I want to remove the 5th line and paste it before the first two lines, to change the buffer into this:

xxxxxxxxline 1
xxxxxxxxline 2
line 3
line 4

How to do that with a simple vim command?

1

Here's a couple of alternative methods. They're both a few more keystrokes than @statox's answer, but I think they're worth posting because they show a few different techniques.

Visual block mode & Ctrl-R

Like @statox, I'm presuming you start (on the first character) of the 5th line for this solution:

First we yank all the x characters (but not the newline):

y$

Then we delete the line:

dd

Then we jump to the top of the file:

gg

Then we enter visual block mode, and move down a line so the beginning of the start of both lines are selected:

Ctrl-Vj

Then we insert at the start of the visual block. When doing this, anything we insert will be inserted on both lines of the block.

I

Next, we enter the contents of the yank register by using insert mode's Ctrl-R feature. (See :help i_CTRL-R):

Ctrl-R0

Finally, we leave insert mode:

Esc

So the complete set of commands is as follows:

y$ddggCtrl-VjICtrl-R0Esc

Ex Commands

Here's a method that only uses the command line. The following commands can be run with the cursor at any position.

First we move line 5 to the top of the file:

:5m0

Then we create another copy of that line:

:1t2

Then we join the lines up:

:1,2norm!Jx

So, the complete set of ex commands is:

:5m0
:1t2
:1,2norm!Jx
| improve this answer | |
  • Instead of yanking, deleting and then using <C-r>0 you could also just delete the lines and use <C-r>" that still shows the usage of <C-r> and save a few keystrokes. – statox May 18 '18 at 15:01
  • @statox but then you have to deal with the newline characters. I’m specifically only yanking the line contents to avoid that. – Rich May 18 '18 at 16:37
  • Of course you are totally right! It was not the smartest comment I've made on this site :) – statox May 18 '18 at 16:39
  • 1
    @statox Haha we've all done it! – Rich May 22 '18 at 10:34
3

You can do the following, starting with your buffer like this and your cursor on the first x of the last line:

line 1
line 2
line 3
line 4
xxxxxxxx

First we copy the last line with Yp

line 1
line 2
line 3
line 4
xxxxxxxx
xxxxxxxx

Your cursor is now on the first x of the new last line, so you enter visual bloc mode with ctrl+v and select the lines with $ (to go to the end of the line) and k to select the line above.

You can then delete the lines with d

line 1
line 2
line 3
line 4

You can then use gg to go to the first character of the first line and paste what you just deleted with P

xxxxxxxxline 1
xxxxxxxxline 2
line 3
line 4

So all in one you can do:

Ypctrl+v$kdggP

See :h visual-block for more details about visual block mode.

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