3

I'm experimenting with vim and got a little stuck with the p command and the way it works.

TEST Data to copy //copy from here

test data to copy //line for experiments

RESULTS:

tTEST est data // cursor at the beginning, "TEST " pasted with p
TEST test data // cursor at the beginning, "TEST " pasted with P

test data      // cursor at the begining, pasted the whole line with p
TEST Data to copy

TEST Data to copy
test data     // cursot at the beginning, pasted the whole line with P

I thought, p pastes after the current cursor position, but pasting a line with it does not work this way.

So, if we paste a word with p, it will be pasted after the character, the cursor points to. But if we yank the whole line it will be pasted after the line the cursor was at when we pressed p.

QUESTION: How does it actually work?

  • 1
    I think you are indeed right that help p's "Put the text [from register x] after the cursor" is not exactly what is happening when the register contains one or more full lines yanked. I'm unaware of a better documentation than the actual help files, though. – Dalker Sep 24 '16 at 13:16
  • @Dalker So we just have to accept that if we have a full line yanked it will work the way it does, right? Some sort of exception, I suppose...... – St.Antario Sep 24 '16 at 13:21
  • With Vim, you can have "characterwise" operations, "linewise" operations or "blockwise-visual" operations (you know, in the same ways you can use a visual selection; this info is stored with the content in a register, e.g. when you yank a line yy etc.) p (and other operations) tries to behave in a "intuitive" manner in each case. When you yank a line, p tries to paste/put "after" the current line, when you yank some chars, p will paste "after" current char etc. – VanLaser Sep 24 '16 at 13:26
7

p and P always work the same way:

  • p puts text after the cursor,
  • P puts text before the cursor.

But what you put with p and P can be "character-wise", "line-wise", or "block-wise". This means that the end result varies not because of p and P but because of the nature of what you put.

Character-wise text

The text you put is "character-wise" if it was yanked with a character-wise motion or from character-wise visual mode.

Examples: yiw, y}, vf)y, etc.

If you put character-wise text with p, the text is put inline, after the cursor:

foo bar baz              source line
   ^^^^                  you yanked " bar"

lorem ipsum dolor        destination line
    ^                    the cursor is on the "m" of "lorem"

lorem bar ipsum dolor    result

With P, the text is put inline, before the cursor:

lore barm ipsum dolor    result

Line-wise text

The text you put is "line-wise" if it was yanked with a line-wise motion or from line-wise visual mode.

Examples: yip, y3j, V25jy, etc.

If you put line-wise text with p, the text is put below the current line:

foo bar baz              source line
^^^^^^^^^^^              you yanked the whole line, including the newline character

lorem ipsum dolor        destination line
    ^                    the cursor is on the "m" of "lorem"

lorem ipsum dolor        result
foo bar baz

With P, the text is put above the current line:

foo bar baz              result
lorem ipsum dolor

Block-wise text

The text you put is "block-wise" if it was yanked from block-wise visual mode.

Example: <C-v>iw6jy, etc.

If you put block-wise text with p, the text is put inline, after the cursor, and overwrites any character in his way:

foo 000 baz              source text
foo 000 bar              the yanked text is the column of zeroes
baz 000 bar

lorem ipsum dolor        destination text
lorem ipsum dolor        the cursor is on the "m" of the first "lorem" 
lorem ipsum dolor

lorem000 ipsum dolor     result
lorem000 ipsum dolor
lorem000 ipsum dolor

With P, the text is put inline, before the cursor, and overwrites any character in his way:

lore000m ipsum dolor     result
lore000m ipsum dolor
lore000m ipsum dolor
  • Why doesn't :%p replace the entire buffer content with the clipboard content ? – Niloct Nov 11 '18 at 22:29
  • You remarked that p and P have always the same behavior, and that differences in what results from their application springs from a difference in what is in the register that is pasted. I think that a great improvement to the answer would be to include the output of the :reg command after the character-wise, line-wise, and block-wise yanks accompained by a comment about why those three different conents of the register translate into the different outcomes of the p command. (By the way, I wouldn't know how to change it, but I think overwrites is not the right verb in your answer.) – Enrico Maria De Angelis Sep 19 at 21:31

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.