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?
To copy text, place the cursor in the desired location and press the y key followed by the movement command. Below are some helpful yanking commands: yy - Yank (copy) the current line, including the newline character. 3yy - Yank (copy) three lines, starting from the line where the cursor is positioned.
Ctrl+W: Cut the word before the cursor, and add it to the clipboard buffer. Ctrl+K: Cut the part of the line after the cursor, and add it to the clipboard buffer. If the cursor is at the start of the line, it will cut and copy the entire line.
yy or Y to copy the line (mnemonic: yank)
or
dd to delete the line (Vim copies what you deleted into a clipboard-like "register", like a cut operation)
then
p to paste the copied or deleted text after the current line
or
P to paste the copied or deleted text before the current line
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 range 1,.
).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 form class xxx
right after the cursor.Reference: :help range
, :help :t
, :help :g
, :help :m
and :help :v
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With