Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 18 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.