95

I'm running vim 7.3 on a Mac 10.7.2 and I'm having some trouble cutting and pasting several lines.

On my old Linux setup (which was stolen so I don't know versions), I could type "dd" multiple times and then "p" would yank all of them back. For example: type: "dd dd" and two lines would be deleted. Now type "p" and both lines are pasted back into the buffer.

I know I can accomplish what I want by typing "2dd", and then "p" - but I would like to be able to "dd"-out lines without counting the number of lines ahead of time.

Any ideas?

alex
  • 6,818
  • 9
  • 52
  • 103
spinlock
  • 3,737
  • 4
  • 35
  • 46
  • 2
    Not to count lines ahead use "relativenumbers" to display line numbers relative to your cursor. And starting with 7.4 setting both "numbers" and "relativenumbers" makes the line number where your cursor is absolute and the others relative, getting the best of both worlds. Then it's easier to do like @Giovanni suggested d2d (for two lines). – Radu Maris Oct 23 '15 at 16:28
  • 1
    This is awesome, but for anyone trying this it is actually "number" and "relativenumber", not "numbers" and "relativenumbers" :) – user3640967 Jan 28 '16 at 15:29

6 Answers6

175

Have you considered using visual mode?

You could just go:

  • Press V
  • Select everything you want to cut without counting
  • Press d
  • Go to where you want to paste
  • Press p

This should yield approximately half as many keystrokes as the dd method since you press one key per line rather than two. Bonus points if you use 5j (or similar) to select multiple lines at a time.

Ben S
  • 68,394
  • 30
  • 171
  • 212
47

You could type:

d<n>d

where <n> is the number of lines that you want to cut, and then you could paste them with:

p

For example, to cut and paste 3 lines:

d3d
p
Giovanni Cappellotto
  • 4,597
  • 1
  • 30
  • 33
20

To cut and paste by line numbers (do :set number to see the line numbers), for lines x to y do:

:x,yd

or if your cursor is already on line x, do

:,yd

Then go to where you want to paste and press p

Kevin Crain
  • 1,905
  • 1
  • 19
  • 28
flazor
  • 201
  • 2
  • 2
7

Not sure if this is close enough to what you're trying, but one thing you could do is use a specific register, and capitalize your register name. That tells vim to append to the register rather than replace it, so if you have the lines:

one
two
three

you can enter

"qdd
"Qdd
"Qdd

and then subsequently if you enter

"qp

it will paste back the original lines

wxs
  • 5,617
  • 5
  • 36
  • 51
5

To copy and paste 4 lines:

y4y (with the cursor on the starting line you wanna copy)

p (with cursor on the line you wanna paste after)

learner
  • 51
  • 1
  • 1
  • 2
    `without counting the number of lines ahead of time.` This solution doesn't help with that. – wxz Apr 28 '21 at 19:45
0

I agree with @Ben S. that this is the preferred way to accomplish this but if you are just looking to replicate your old behavior you can remap dd to append to a specified register, and then map p to paste from that register and clear it.
This will have the disadvantage of causing p to only work with things deleted using dd (using d} to delete to the end of the paragraph would not put the text in the correct register to be pasted later).

Add the following to your vimrc

noremap dd "Ddd             "Appends the contents of the current line into register d
noremap p "dp:let @d=""<CR> "Pastes from register d and then clears it out

if you don't want pasting to clear out the contents of the register

noremap p "dp               "Paste from register d 

but this will cause that register to grow without ever clearing it out

Sam Brinck
  • 891
  • 1
  • 7
  • 11