14

Suppose I have the following four lines:

line1
line2
line3
line4

Now if I go to line1 and do a yy which copies the line and then go to start of line3 and do a paste using 'p', I would get

...
line3
line1
line4
....

Why did it paste line1 after line3 in a new line?

My initial though was because the newline character $ was attached to line3 and that when I pressed p vim considered line3$ as one word so it pasted after line3$. However it seems my understanding was wrong. Because if I do this line3 $ and then do a p at the start of the line I get the same result.

Why does p create a new line and paste in a new line after line3?

1 Answer 1

23

When you deleted the line using dd, you performed a linewise delete.

The p command pastes after the cursor position. Since the default register was populated with linewise content, that means it will paste after the line the cursor is on.

If you had instead deleted the contents of the line in a characterwise manner (e.g., 0D), then the register would have been characterwise and therefore pasted after the character the cursor was on.

In general, the behavior should follow what be expected from a delete/yank and paste. However, it is possible to modify the -wiseness of a register using getreg()/setreg().

Be careful doing that, though. Given your initial example, simply changing the register to be characterwise doesn't remove the newline that was part of the delete. It just changes how the paste occurs.


For recommendations on the specific scenario of deleting an entire line but pasting it characterwise, see this post.

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.