How to yank the text on a line and paste it inline in Vim?

Use yiw ("yank inner word") instead of yy to yank just what you want:

yy is line-wise yank and will grab the whole line including the carriage return, which you can see if you look at the unnamed register ("") in :registers which is used as the source for pastes. See :help "":

Vim uses the contents of the unnamed register for any put command (p or P) which does not specify a register. Additionally you can access it with the name ". This means you have to type two double quotes. Writing to the "" register writes to register "0.

An additional benefit to yiw is that you don't have to be at the front of the "word" you are yanking!


The problem is that yy is copying the entire line, including the newline. An alternative would be to copy from the beginning to the end of the line, and then paste.

^y$

  • ^ Go to the first character of the line.
  • y Yank till
  • $ End of line.

// Credit to: tester and Idan Arye for the Vim golf improvements.

Tags:

Vim