Vim: Replacing a line with another one yanked before
VimVim 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 :
aG
Y
xG
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
- yy
- j (move to the line you want to replace),and then
- 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
-
Move to the start of the first line.
-
y, $ – copy the line without the linebreak at the end
-
Move to the start of the target line.
-
-
V, p – replace just one target line
-
c, c, Ctrlr, 0, Esc – replace the target line with the original yank
-
-
Move to the start of the next target line.
-
. – 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 Ctrlr, 0 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:
A
G - Select the line with visual mode: VESC
- go to line X:
X
G - 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_
-
Copy the 1st line (
:t
command is an alias for:copy
) after line 20 (will place it on line 21) -
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
- :ay (where a is the line number. Example :20y). This yanks a line(pun intended).
- 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