Vim – How to paste a inner-word to line above

vim

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?

Best Answer

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

Related Question