6

I cc or dd to copy a line into my paste buffer. Then I go to a new location.

I want to replace a line that I see. So I type p Down dd.

Is there an easier way (fewer keystrokes)?

  • Hmm, for one off edits, that's how I usually do it. I'd be interested in any shorter way as well. – jw013 Jul 14 '12 at 18:38
  • You can type V to select the line, then p to replace it. – angus Jul 14 '12 at 18:45
  • @angus, turn that comment into an answer! It's a good answer. – Jander Jul 14 '12 at 19:26
8

You can type V to select the line, then p to replace it.

  • I think this is the only anwer I've seen that actually has less keystrokes than dd, move, pjdd. – jw013 Jul 15 '12 at 4:15
3

Another way to do this would be "_ddp

"_dd deletes the current line to the null-buffer. This doesn't over write what you had just copied, which I have found helpful a lot of times! The p pastes the line you had copied earlier.

Might not be shorter in terms of keystrokes, but knowing this is an option can be very helpful!

2

Angus and Thor have the short answers covered, so here's some bonus exploration.

If for some reason you want to delete before pasting, you can take advantage of the numbered registers. The "0 register contains the text yanked by the last y or Y, but not delete. The "1 through "9 registers store the last 9 deletes/cuts you've done. So:

  • If you yanked with yy, you could replace with dd"0P.
  • If you cut with dd or cc, you could replace with dd"2P. (Not dd"1P because the dd fills "1.)

Try :help "0 for more in-depth info.

1

Suppose you want to replace line 1 by 3 than you could do :1m3|2d

This effectively moves line 1 to line 3, and removes line 2 (i.e. the old line 3)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.