Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Replacing the End of the Line

Tags:

vim

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.

like image 911
Rafid Avatar asked Aug 27 '10 15:08

Rafid


People also ask

How do I change a whole line in vim?

Use c$ or just C to quickly change from the cursor to the end of a line, cc to change an entire line, or cis for a sentence. The standard change word command requires you to type cw , then a new word, then press Escape.

How do I move the last line in vi?

Press ^ to move the cursor to the start of the current line. Press $ to move the cursor to the end of the current line.

How do you replace a phrase in vim?

The simplest way to perform a search and replace in Vim editor is using the slash and dot method. We can use the slash to search for a word, and then use the dot to replace it. This will highlight the first occurrence of the word “article”, and we can press the Enter key to jump to it.


2 Answers

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.
like image 153
Nefrubyr Avatar answered Oct 13 '22 23:10

Nefrubyr


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.

like image 30
wuputah Avatar answered Oct 13 '22 23:10

wuputah