Search code examples
vimpasteyank

Why does yyp not behave according to p's documentation


I was just wondering if someone could explain what goes on behind the scenes w.r.t the behaviour of yyp.

Given some text:

Text that can be copied

typing yyp while the cursor is in the middle of a line, let's say just before can, results in:

Text that can be copied
Text that can be copied

Which, while very useful and logical, is not what p typically does.

As p pastes after the cursor, and my cursor remains before can, I don't feel I would be wrong to expect something like:

Text that Text that can be copied can be copied

While I agree the former is more useful – could someone explain why and how in these instances the default behaviour of p is ignored?


Solution

  • This is explained at :help linewise-register. Vim motions either cover a sequence of characters or whole lines. Likewise, text that is yanked into a register either is comprised of characters (including newlines, but not ending with one), complete lines (always ending with a newline), or a block of text (from a <C-V> blockwise visual selection). When you paste, the "insertion point" is determined by the source register, so complete lines will be pasted on separate lines. The :reg command indicates the type for each register with c / l / b in the first column.

    :reg abc
    Type Name Content
    c  "a   a word
    l  "b   a line^J
    b  "c   a block^Jof text
    

    I agree with you that the default behavior is useful. Sometimes, it is helpful to override that, though, e.g. to paste an incomplete text as a separate line, or to insert a full deleted line inside an existing line. Vim offers ways to insert the register contents they way you want, but you have to remember it and it's several keys to type. I needed to "cast" register contents into a certain (characterwise / linewise / blockwise) mode so often, I wrote the UnconditionalPaste plugin for it. It provides gcp, glp, etc. alternatives to the built-in paste commands that force a certain mode (and by now several more variations on this theme, like pasting with joined by commas or queried characters).