How to duplicate a whole line in Vim?

VimEditorKeyboard ShortcutsVi

Vim Problem Overview


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?

Vim Solutions


Solution 1 - Vim

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

Solution 2 - Vim

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

Solution 3 - Vim

YP or Yp or yyp.

Solution 4 - Vim

copy and paste in vim

Doesn't get any simpler than this! From normal mode:

yy

then move to the line you want to paste at and

p

Solution 5 - Vim

yy

will yank the current line without deleting it

dd

will delete the current line

p

will put a line grabbed by either of the previous methods

Solution 6 - Vim

Do this:

First, yy to copy the current line, and then p to paste.

Solution 7 - Vim

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.

Solution 8 - Vim

yyp - remember it with "yippee!"

Multiple lines with a number in between:

y7yp

Solution 9 - Vim

yyp - paste after

yyP - paste before

Solution 10 - Vim

I like: Shift+v (to select the whole line immediately and let you select other lines if you want), y, p

Solution 11 - Vim

Another option would be to go with:

nmap <C-d> mzyyp`z

gives you the advantage of preserving the cursor position.

Solution 12 - Vim

You can also try <C-x><C-l> which will repeat the last line from insert mode and brings you a completion window with all of the lines. It works almost like <C-p>

Solution 13 - Vim

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

Solution 14 - Vim

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.

Solution 15 - Vim

Default is yyp, but I've been using this rebinding for a year or so and love it:

nnoremap Y yyp
vnoremap Y y`>pgv

Solution 16 - Vim

1 gotcha: when you use "p" to put the line, it puts it after the line your cursor is on, so if you want to add the line after the line you're yanking, don't move the cursor down a line before putting the new line.

Solution 17 - Vim

For those starting to learn vi, here is a good introduction to vi by listing side by side vi commands to typical Windows GUI Editor cursor movement and shortcut keys. It lists all the basic commands including yy (copy line) and p (paste after) or P(paste before).

vi (Vim) for Windows Users

Solution 18 - Vim

If you would like to duplicate a line and paste it right away below the current like, just like in Sublime Ctrl+Shift+D, then you can add this to your .vimrc file.

nmap <S-C-d> <Esc>Yp

Or, for Insert mode:

imap <S-C-d> <Esc>Ypa

Solution 19 - Vim

I prefer to define a custom keymap Ctrl+D in .vimrc to duplicate the current line both in normal mode and insert mode:

" duplicate line in normal mode:
nnoremap <C-D> Yp
" duplicate line in insert mode:
inoremap <C-D> <Esc> Ypi

Solution 20 - Vim

I like to use this mapping:

:nnoremap yp Yp

because it makes it consistent to use alongside the native YP command.

Solution 21 - Vim

I use this mapping, which is similar to vscode. I hope it is useful!!!.

nnoremap <A-d> :t. <CR>==
inoremap <A-d> <Esc>:t. <CR>==gi
vnoremap <A-d> :t$ <CR>gv=gv

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsumekView Question on Stackoverflow
Solution 1 - VimMark BiekView Answer on Stackoverflow
Solution 2 - VimBenoitView Answer on Stackoverflow
Solution 3 - VimLinulinView Answer on Stackoverflow
Solution 4 - VimAdamView Answer on Stackoverflow
Solution 5 - VimpjzView Answer on Stackoverflow
Solution 6 - VimEric Z BeardView Answer on Stackoverflow
Solution 7 - VimKwondriView Answer on Stackoverflow
Solution 8 - VimtheschmitzerView Answer on Stackoverflow
Solution 9 - VimyemuView Answer on Stackoverflow
Solution 10 - VimGabeView Answer on Stackoverflow
Solution 11 - VimRookView Answer on Stackoverflow
Solution 12 - VimnikolavpView Answer on Stackoverflow
Solution 13 - VimMichaelView Answer on Stackoverflow
Solution 14 - VimDarkWiiPlayerView Answer on Stackoverflow
Solution 15 - VimChris PennerView Answer on Stackoverflow
Solution 16 - VimcoriView Answer on Stackoverflow
Solution 17 - Vimap-osdView Answer on Stackoverflow
Solution 18 - VimjediView Answer on Stackoverflow
Solution 19 - VimSavrigeView Answer on Stackoverflow
Solution 20 - VimyolenoyerView Answer on Stackoverflow
Solution 21 - VimfrfernandezdevView Answer on Stackoverflow