How do I duplicate a whole line in Vim in a similar way to Ctrl+D in IntelliJ IDEA/ Resharper or Ctrl+Alt+↑/↓ in Eclipse?
20 Answers
Normal mode: see other answers.
The Ex way:
:t.
will duplicate the line,:t 7
will copy it after line 7,:,+t0
will copy current and next line at the beginning of the file (,+
is a synonym for the range.,.+1
),:1,t$
will copy lines from beginning till cursor position to the end (1,
is a synonym for the range1,.
).
If you need to move instead of copying, use :m
instead of :t
.
This can be really powerful if you combine it with :g
or :v
:
:v/foo/m$
will move all lines not matching the pattern “foo” to the end of the file.:+,$g/^\s*class\s\+\i\+/t.
will copy all subsequent lines of the formclass xxx
right after the cursor.
Reference: :help range
, :help :t
, :help :g
, :help :m
and :help :v
If you want another way:
"ayy
:
This will store the line in buffer a
.
"ap
:
This will put the contents of buffer a
at the cursor.
There are many variations on this.
"a5yy
:
This will store the 5 lines in buffer a
.
See "Vim help files for more fun.
For someone who doesn't know vi, some answers from above might mislead him with phrases like "paste ... after/before current line".
It's actually "paste ... after/before cursor".
yy or Y to copy the line
or
dd to delete the line
then
p to paste the copied or deleted text after the cursor
or
P to paste the copied or deleted text before the cursor
For more key bindings, you can visit this site: vi Complete Key Binding List
I know I'm late to the party, but whatever; I have this in my .vimrc:
nnoremap <C-d> :copy .<CR>
vnoremap <C-d> :copy '><CR>
the :copy
command just copies the selected line or the range (always whole lines) to below the line number given as its argument.
In normal mode what this does is copy .
copy this line to just below this line.
And in visual mode it turns into '<,'> copy '>
copy from start of selection to end of selection to the line below end of selection.
Y
esP
lease. :) – Stavr00