Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

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?

like image 880
tester Avatar asked Oct 14 '11 21:10

tester


People also ask

How do I paste a yanked line in Vim?

To put the yanked or deleted text, move the cursor to the desired location and press p to put (paste) the text after the cursor or P to put (paste) before the cursor.

How do you paste text into yanked?

Yes. Hit Ctrl - R then " . If you have literal control characters in what you have yanked, use Ctrl - R , Ctrl - O , " .

How do I paste a yanked line in Vim to Notepad?

In Vim, Copying is done using the y or "yanking". To copy selected text to system clipboard type "+y in Normal Mode. Now you can paste it anywhere else using Ctrl-v .


2 Answers

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.

like image 82
4 revs, 3 users 80% Avatar answered Sep 25 '22 13:09

4 revs, 3 users 80%


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!

like image 30
bheeshmar Avatar answered Sep 22 '22 13:09

bheeshmar