Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut and paste multiple lines in vim

Tags:

vim

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?

like image 282
spinlock Avatar asked Jan 24 '12 00:01

spinlock


People also ask

How do you copy and paste multiple lines in Vim?

Press v to select characters, or uppercase V to select whole lines, or Ctrl-v to select rectangular blocks (use Ctrl-q if Ctrl-v is mapped to paste). Move the cursor to the end of what you want to cut. Press d to cut (or y to copy). Move to where you would like to paste.

How do I cut and paste lines in Vim?

You can use a movement command or up, down, right, and left arrow keys. Press y to copy, or d to cut the selection. Move the cursor to the location where you want to paste the contents. Press P to paste the contents before the cursor, or p to paste it after the cursor.


1 Answers

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.

like image 74
Ben S Avatar answered Sep 17 '22 16:09

Ben S