tags:

views:

101

answers:

6

In Vim, how is it possible to replace the remaining of a line with the yanked text? If I press "D", the yanked text will be replaced with the deleted text.

Also, how is it possible to yank to the end of the line? If I press "Y", it will yank the whole line.

A: 
y$ - yank to end of line
codaddict
@Downvoter: care to explain ?
codaddict
+2  A: 

Press v to enter visual mode on the starting character of the selection. Then hit $ to go to the end of the line. After that, hit y to yank the selection.

So:

v$y

Instead of yanking, use p to paste. This will paste over the selection. So after you use D to delete some text, do:

v$p

This will paste the deleted text over the selection.

Vivin Paliath
Yeah, that will yank the selection, but I am looking for away to replace the selected text (or the text to the end of the line) with the yanked text.
Rafid K. Abdullah
Oh, sorry, I thought you meant press v$y to yank. Yeah, that works, thanks.
Rafid K. Abdullah
+10  A: 

Two ways you can replace text to the end of the line with previously yanked text:

  1. v$pv to enter visual mode; $ to move to end of line; p to paste over highlighted text.
  2. D"0p — the last yanked (as opposed to deleted) text is stored in register 0, so: D to delete to end of line; "0 to select register 0; p to paste that register.
Nefrubyr
Didn't know about register `0`. That's pretty cool.
wuputah
+1 - didn't know about the register.
Vivin Paliath
Yeah, I also didn't know about the register comment.Is there anyway to select two answers as the accepted answer? :)
Rafid K. Abdullah
+2  A: 

Your questions seems to be thoroughly answered by others at this point (v$p and y$), but I wanted to one additional piece of information:

To yank to end of line, the default way is y$. However, it is a fairly common practice to map Y y$ in your .vimrc, since the default behavior of Y is redundant with yy, and is inconsistent with other mappings, like D and C.

wuputah
Thanks for this comment, it is very useful. I will add that to my .vimrc :)
Rafid K. Abdullah
A: 

Here's how I'd do it:

  1. "ay$ (yank from cursor to end of line into register a)
  2. Move cursor to desired replace position.
  3. D (delete to the end of the line)
  4. "ap (paste the text yanked in step 1)

Admittedly, this is very similar to another answer but I prefer explicitly specifying a register because it forces me to think about what I'm doing. I could see myself accidentally overwriting register 0 if I got distracted or tried to do something else.

Kristo
+1  A: 

If you want to delete text without affecting the main (unnamed) register, delete your text into the "black hole register", called "_. Do this via "_D. Then the unnamed register will be unaffected and you can paste your previously-yanked text as normal.

See :h quote_.

Brian Carper