I always use the following commands to yank an inner word and then paste it in the line above: yiw -> O -> Esc -> p

Obviously P by itself (without using O to insert a line above) doesn't work, because there's no new line character, so instead that just pastes it before the cursor.

Is there an easier way to do this?


Two suggestions to paste the contents on a line of its own:

  1. You can use the :put! command, since it always works linewise. The version with the ! inserts the contents of the register before (rather than after) the current line. (You can abbreviate it to :pu!.)

  2. You can use O, Ctrl+R, ", Esc to insert a line above with the contents of the latest yank. See help on i_CTRL-R for the Ctrl+R part. And " is the "unnamed" register, which is where yanks and deletes go by default. This is not necessarily "easier" than O, Esc, p, but it has the advantage that it's a single command, so it's repeatable with . and the whole action can be undone at once.

If this is a frequent enough operation for you, consider creating a mapping for it, that would be surely the easiest one to type. :-)

  • I think [P or a similar command forces linewise putting. (The think is because i use tpope’s unimpaired which maps that key, and I can’t remember the exact original linewise-put key.) – D. Ben Knoble Jun 5 '19 at 13:07

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

With it, you can just use glp / glP to paste after / before the current line, regardless of how you've yanked the text (i.e. also when it just contains an incomplete line fragment).

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.