Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Replacing a line with another one yanked before

Tags:

vim

People also ask

How do I replace a word with yanked in Vim?

Move the cursor to another word (say "third"). Repeat the operation (change word and replace it with "first"). Move the cursor to another word and press . to repeat the change. Yank inner text (text containing cursor which is in quotes).

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 we search for old and replace it with new in vim?

To search in the current file, you just need to type / in normal mode. Then, you need to type your search pattern, press enter , and the result becomes highlighted in your file. To go backward and forward through the results, you can type n and N (for n ext) respectively. Using / will search forward in the file.

How do you substitute in vi?

Press y to replace the match or l to replace the match and quit. Press n to skip the match and q or Esc to quit substitution. The a option substitutes the match and all remaining occurrences of the match. To scroll the screen down, use CTRL+Y , and to scroll up, use CTRL+E .


Vp: select line, paste what was yanked


What I would do :

  1. aG
  2. Y
  3. xG
  4. Vp

You don't have to leave normal mode, but it does yank the line. You can however use V"0p which will always put the line yanked in step 2.


This has the additional disadvantage that line X is now in the default register, which is annoying if I find another line that should be replaced with A.

To delete text without affecting the normal registers, you can use the Black hole register "_:

"_dd

Building on the answers that suggest using Vp or VP to paste over a line -- to avoid changing the contents of the yank register I find the most ergonomic command is simply:

VPY


  1. yy
  2. j (move to the line you want to replace),and then
  3. Vp (uppercase v and then p, will replace with the yanked content)

I would use commandline (Ex) mode and do the following two commands

:XmA
:Ad

This simply moves line X to just under A, then deleting A moves that line up

For example

:7m3
:3d