I normally copy a line pressing 'y' key twice, then pressing 'p' or 'P' to paste after or before a current line, respectively. Sometimes, however I need to replace a current line with the yanked line. How to do it?
Asked
Active
Viewed 2,980 times
4 Answers
38
Pasting over a visual selection should work: V p
(V to select the entire line visually, p to replace it with the contents of the default register).
Prince Goulash
- 15,295
- 2
- 46
- 47
-
3
-
3Nice, you've just saved me a few extra minutes per year! Only thing to note is that the contents of the default register are replaced. – YXD May 26 '11 at 15:10
-
-
9A slightly safer version would be: `V"0p`, which pastes from the yank register instead of the default register. As Mr E noted, the visual selection overwrites the default register, so if you ran `Vp` twice it might produce different results each time. – nelstrom May 27 '11 at 12:18
-
Good thinking, nelstrom. I am forever putting brevity ahead of safety! – Prince Goulash May 27 '11 at 12:29
-
This answer should be updated (with credits) according to the comment by @nelstrom – mloskot Nov 22 '22 at 16:20
5
You can delete the current line without replacing your copied/yanked line with "_dd.
YXD
- 31,741
- 15
- 75
- 115
-
Yes - I saw it on a forum a while back. Prince Goulash has a much better solution though! – YXD May 26 '11 at 15:09
0
You can also just turn off the buffer-overwrite side-effect behavior as needed by putting this script in your .vimrc
vim toggling buffer overwrite behavior when deleting
Then you can toggle the overwrite behavior using key combo ,, (two commas)
0
Delete the line before or after yanking the (possibly named) buffer. Delete after if you didn't name yank into a named buffer - or use "2P (or "2p) to yank the second buffer if you delete first.
Jonathan Leffler
- 730,956
- 141
- 904
- 1,278