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

Vim

Vim Problem Overview


Say, I have the following lines:

thing();
getStuff();

I want to take getStuff() using the yy command, go forward to thing(), placing the cursor on (, and paste via the p command, but since I yanked the whole line, p will paste getStuff() right back where it was.

I know you can first move the cursor to the beginning of that getStuff() line and cut the characters from there until its end via the ^D commands—then p will do what I want. However, I find typing ^D to be much more tedious than yy.

Is there a way to yy, but paste the line inline instead?

Vim Solutions


Solution 1 - Vim

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.

Solution 2 - 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!

Solution 3 - Vim

One way to simplify the routine of operating on the same text patterns is to define mappings that mimic text-object selection commands.

The two pairs of mappings below—one for Visual mode and another for Operator-pending mode—provide a way to select everything on the current line except for the new line character (al), and everything from the first non-blank character of the current line through the last non-blank character, inclusively (il).

:vnoremap <silent> al :<c-u>norm!0v$h<cr>
:vnoremap <silent> il :<c-u>norm!^vg_<cr>
:onoremap <silent> al :norm val<cr>
:onoremap <silent> il :norm vil<cr>

Thus, instead of using yy to copy the contents of a line that is to be pasted character-wise (and not line-wise), one can then use the yal or yil commands to yank, followed by the p command to paste, as usual.

Solution 4 - Vim

A less efficient, but simple method:

  • v to highlight the word(s),
  • y to yank the highlighted word(s),
  • p (at the end of the line) you want to paste

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontesterView Question on Stackoverflow
Solution 1 - VimTabithaView Answer on Stackoverflow
Solution 2 - VimbheeshmarView Answer on Stackoverflow
Solution 3 - Vimib.View Answer on Stackoverflow
Solution 4 - VimwulfgarproView Answer on Stackoverflow