Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to duplicate a whole line in Vim?

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?

like image 568
sumek Avatar asked Sep 16 '08 15:09

sumek


People also ask

How do I copy a whole line in Vim?

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.

How do I copy an entire line in Linux?

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.


2 Answers

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

like image 184
Mark Biek Avatar answered Sep 17 '22 19:09

Mark Biek


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

like image 44
Benoit Avatar answered Sep 20 '22 19:09

Benoit