Cut and paste multiple lines in 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?
Solution 1:
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.
Solution 2:
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
Solution 3:
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