Vim Trick: How to Completely Replace a Line With Copied Text
Tue Oct 24 2017 | Tricks | 0 CommentsHave you ever found yourself doing the following:
Copying a line (
yy
)Moving down a few lines (
j
)Pasting in your copied line above the line you're trying to replace (
p
)Moving to the line you were trying to replace and deleting it (
dd
)Getting frustrated because you want to replace another line with your original line somewhere else, but realize that your buffer is now filled with the line you just deleted so you now have to copy the original line again (
yy
) before starting the whole process over again.
Don't worry. There's a better way.
While the keystrokes for this trick aren't quite as finger-friendly as I would like, it still saves a ton of time and frustration.
Y
(shortcut foryy
)Move to the line you want to replace
V"0p
Repeat steps 2 and 3 indefinitely
Breaking it down
Y
copies (yanks) the entire lineV
switches to visual mode and highlights the entire line"
is how you access Vim's registers. Registers are basically clipboards Vim uses to save data for later. For more info on registers visit http://www.brianstorti.com/vim-registers0
is a special register Vim uses to store your last yanked textp
pastes the previously specified register