Vim: Replacing a line with another one yanked before

Vim

Vim Problem Overview


At least once per day i have the following situation:

A: This line should also replace line X
...
X: This is line should be replaced

I believe that I don't perform that task efficiently.

What I do:

  • Go to line A: AG
  • Yank line A: yy
  • Go to line X: XG
  • Paste line A: P
  • Move to old line: j
  • Delete old line: dd

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. Yanking to and pasting from an additional register ("ayy, "aP) makes this simple task even less efficient.

My Questions:

  • Did I miss a built-in Vim command to replace a line yanked before?
  • If not, how can I bind my own command that leaves (or restores) the yanked line in the default register?

Vim Solutions


Solution 1 - Vim

Vp: select line, paste what was yanked

Solution 2 - Vim

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.

Solution 3 - Vim

> 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

Solution 4 - Vim

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

Solution 5 - Vim

  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)

Solution 6 - Vim

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

Solution 7 - Vim

  1. Move to the start of the first line.

  2. y, $ – copy the line without the linebreak at the end

  3. Move to the start of the target line.

    1. V, p – replace just one target line

    2. c, c, Ctrlr, 0, Esc – replace the target line with the original yank

  4. Move to the start of the next target line.

  5. . – repeats the command issued at 4.2.

Notes:

  • 4.1 is y, $ because if you do y, y or Y you will copy the linebreak, and Ctrlr0 actually adds the linebreak below your target line.

  • 4.2 replaces V p, which doesn’t work with repeat because technically the last action is delete, so . would just delete a line.

  • If anyone knows how to issue ‘replace current line with register’ from EX mode (command line), I would like to hear from you (and to know where you found the documentation). There may be a repeatable EX command which is faster than 4.2 and/or does not have the linebreak caveat.

Solution 8 - Vim

You can also do:

Vy (in normal mode at the line you want to copy)
Vp (in normal mode at the line you want to replace)
  • Doesn't create spaces or line ends.
  • Cursor is placed at the start of the copied text.

The same keys can be used to yank/paste more than one line.

V (in normal mode at what you want to yank)
(use jk to move the selection)
y (to yank the selection)
V (in normal mode at where you want to paste)
(use jk to move the selection)
p (to replace the selection with the yanked lines)

Solution 9 - Vim

You can use this with visual mode.

  • Go to line A: AG
  • Select the line with visual mode: VESC
  • go to line X: XG
  • Enter substitute mode for the line: S
  • Paste the line you copied: shift+insert (or whatever other you mapping you have for pasting from the clipboard).

Solution 10 - Vim

Here's what I would do

  • Move beginning of line A, AG (where A is a line number obviously)
  • Yank line to some register, e.g. a (without new line). Type "ay$
  • Move to insert line, XG
  • Substitute line, S
  • Insert from register a, Ctrl-Ra

Solution 11 - Vim

In light of the recent comment by cicld (thank you!), I see that I didn't grasp the original issue fully. Moving the line is not appropriate, but copying is (since the line is yanked.) So I would revise it to:

:1t20:20d_
  1. Copy the 1st line (:t command is an alias for :copy) after line 20 (will place it on line 21)

  2. Delete line 20, putting the deleted line into the 'blackhole' register (_) (i.e. not affecting the current yank buffer)

As mentioned in the recent comment, this will not affect the current cursor position.

Solution 12 - Vim

You can use this commands in Normal Mode:

:AmX | Xd

the m command is for m[ove], which moves the line number A after the line number X, if you want to copy instead of move the line, use co[py]. the d command is for d[elete].

You can move(copy using co) a range of lines using

:start,end m X

Solution 13 - Vim

  1. :ay (where a is the line number. Example :20y). This yanks a line(pun intended).
  2. Vp

Solution 14 - Vim

I find it easier to use Ex command for this; ex. to move line 9 to 46:

 :46|9m.|-1d

This will move the cursor to line 46, move line 9 below the current, then delete the previous line (since moved line is the current one).

Or using mark(s), using mark 'a':

:46ma a|9m'a|'ad

Solution 15 - Vim

I often have to Y one line and replace it in multiple places, each of which have a different value (which means that I can't do a regex).

Y to yank the desired original line

and then on every line that you'd like to replace, VpzeroY

Solution 16 - Vim

i would simple use the "Black hole" register:

given:

nnoremap < C-d > "_dd

the solution would be:

< C-d >yy

Solution 17 - Vim

If you only want to change part of the line you can do that this way:

Move to position of what part of text you want to copy

y,$ - Yank from cursor to EndOfLine

move to position where you want to replace

v,$,p - replace from cursor to EndOfLine with contents of register

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
QuestioncrispyView Question on Stackoverflow
Solution 1 - VimDorianView Answer on Stackoverflow
Solution 2 - VimicecrimeView Answer on Stackoverflow
Solution 3 - VimEugene YarmashView Answer on Stackoverflow
Solution 4 - VimRichardWView Answer on Stackoverflow
Solution 5 - VimMariano AnayaView Answer on Stackoverflow
Solution 6 - VimericgamlielView Answer on Stackoverflow
Solution 7 - VimJordan MorrisView Answer on Stackoverflow
Solution 8 - VimKozmonautView Answer on Stackoverflow
Solution 9 - VimNathan FellmanView Answer on Stackoverflow
Solution 10 - VimBrian RasmussenView Answer on Stackoverflow
Solution 11 - VimnkalviView Answer on Stackoverflow
Solution 12 - VimNafaa BouteferView Answer on Stackoverflow
Solution 13 - VimBharadView Answer on Stackoverflow
Solution 14 - Vimnk3181544View Answer on Stackoverflow
Solution 15 - VimRonView Answer on Stackoverflow
Solution 16 - Vimj5shiView Answer on Stackoverflow
Solution 17 - VimGregoryView Answer on Stackoverflow