1745

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?

sumek
  • 24,063
  • 10
  • 50
  • 64
  • 53
    Do people not run vimtutor anymore? This is probably within the first five minutes of learning how to use Vim. – dash-tom-bang Feb 15 '16 at 23:31
  • 150
    Would you like to duplicate this line? `Y`es `P`lease. :) – Stavr00 Jan 26 '17 at 15:56
  • 5
    FWIW I have done vimtutor about a dozen times in the last 2 months and this concept is not covered. It tells how to do “dd” and “v - navigate - y” followed by “p”. It does not tell how to copy a single line without deleting it as is asked here. – danielson317 Nov 17 '19 at 16:48

20 Answers20

2940

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

iono
  • 2,418
  • 1
  • 25
  • 35
Mark Biek
  • 135,050
  • 52
  • 150
  • 195
  • 137
    An excellent point. For some reason though, I find hitting y twice is faster for me than SHIFT-y – Mark Biek Oct 06 '08 at 12:35
  • 27
    @camflan I think the Y should be "copy from the cursor to the end" – Dzung Nguyen Jul 19 '12 at 11:35
  • 43
    and 2yy can be used to copy 2 lines (and for any other n) – Amir Ali Akbari Oct 09 '12 at 10:33
  • 6
    @nXqd: Yes, a big percentage of users maps `Y` to `y$`. (Consistent with `D` and `C`; (but not Vi compatible (no one cares.))) That is even proposed in [`:help Y`](http://vimhelp.appspot.com/change.txt.html#Y). – Aaron Thoma Jan 19 '14 at 02:48
  • 11
    To copy two lines, it's even faster just to go `yj` or `yk`, especially since you don't double up on one character. Plus, `yk` is a backwards version that `2yy` can't do, and you can put the number of lines to reach backwards in `y9j` or `y2k`, etc.. Only difference is that your count has to be `n-1` for a total of `n` lines, but your head can learn that anyway. – zelk Mar 09 '14 at 13:29
  • 2
    Also, use: yj to copy current line and line below it / yk to copy current line and line above it – Michael Goldshteyn May 01 '14 at 17:51
  • 1
    Well, it's not the same behavior as in IntelliJ IDEA where the caret remains on the same place. Either `p`, `P` or `gp` will reset caret location to column 1 – Steven Pribilinskiy Dec 02 '14 at 20:15
  • To overwrite the target line: `Vp` – lukas_o Nov 27 '15 at 16:30
  • If this didn't work for you, you've copied someone else's .vim files and they must not know how to use Vim either. – dash-tom-bang Feb 15 '16 at 23:32
  • This does not seem to preserve the cursor position at all, it moves my cursor to the beginning of the line. – Adam Hunyadi Jun 04 '17 at 13:40
  • For some reason "y y" did not work in exc mode. However, "y 1 y" works! – Michael Dimmitt Jun 21 '17 at 16:39
  • `nnoremap :copy .` (read: map Ctrl-D to duplicate the current line in normal mode) `nnoremap yyp` (reda: map Ctrl-D to copy the current line and paste it as a new line below) – DarkWiiPlayer Apr 13 '18 at 07:15
434

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

Hjulle
  • 2,018
  • 1
  • 19
  • 31
Benoit
  • 70,220
  • 21
  • 189
  • 223
  • do you know if`s there a way to do this in visual mode? One of my most common patterns of usage is like visual select 3 lines + :d or :y, for example.. – Breno Salgado Jun 30 '12 at 00:53
  • 1
    When you press `:` in visual mode, it is transformed to `'` so it pre-selects the line range the visual selection spanned over. So, in visual mode, `:t0` will copy the lines at the beginning. – Benoit Jun 30 '12 at 14:17
  • 1
    For the record: when you type a colon (:) you go into command line mode where you can enter Ex commands. http://vimdoc.sourceforge.net/htmldoc/cmdline.html Ex commands can be really powerful and terse. The yyp solutions are "Normal mode" commands. If you want to copy/move/delete a far-away line or range of lines an Ex command can be a lot faster. – Niels Bom Jul 31 '12 at 08:21
  • 7
    Downvoted not due to a problem with the answer as such (although it wouldn't work for my situation, I have no idea the line number I want to duplicate to) but because it REALLY shouldn't be the top / accepted answer for this commonly searched question. – mjaggard Dec 12 '12 at 12:57
  • 9
    @mjaggard: accepted answers are always at the top, regardless of their score. Yes I added that answer as a complement, and it seems it suited the OP well. – Benoit Dec 12 '12 at 14:57
  • 8
    `:t.` is the exact answer to the question. – Burak Erdem Jul 08 '16 at 16:55
  • If you're using relative numbers use `:t +7` or `:t -7` – Evmorov Jan 15 '17 at 23:24
  • @mjaggard, turn on line numbers with `:set number`. I use `:t` every day, usually copying a line or a range of lines from somewhere else in my buffer (usually with a split so I can see exactly which line number they are, to the line where my cursor is. E.g. if I'm writing a presentation in markdown and want to copy headings across multiple slides, `:367t.`, copy line 367 and put it here. – Jangari Jul 25 '18 at 00:55
  • vim and it's undisputed awesomeness ... – George Udosen Mar 04 '21 at 11:05
308

YP or Yp or yyp.

Tim Cooper
  • 144,163
  • 35
  • 302
  • 261
Linulin
  • 3,779
  • 1
  • 17
  • 12
273

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
Adam
  • 2,989
  • 1
  • 16
  • 7
54

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

Arslan Ali
  • 16,294
  • 7
  • 51
  • 65
pjz
  • 38,171
  • 5
  • 45
  • 60
46

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.

the Tin Man
  • 150,910
  • 39
  • 198
  • 279
Kwondri
  • 495
  • 3
  • 4
44

Do this:

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

Arslan Ali
  • 16,294
  • 7
  • 51
  • 65
Eric Z Beard
  • 35,488
  • 25
  • 97
  • 144
  • 1
    Yes, if the cursor is at the end of the line and you type the space as shown you'll duplicate the line you yanked a 2 lines below the line you yanked. –  Jan 09 '14 at 10:56
42

yyp - remember it with "yippee!"

Multiple lines with a number in between:

y7yp

Mwiza
  • 4,494
  • 2
  • 33
  • 30
theschmitzer
  • 10,928
  • 11
  • 37
  • 48
26

yyp - paste after

yyP - paste before

Mwiza
  • 4,494
  • 2
  • 33
  • 30
yemu
  • 18,591
  • 8
  • 25
  • 29
16

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

Xavier Guihot
  • 32,132
  • 15
  • 193
  • 118
11

Another option would be to go with:

nmap <C-d> mzyyp`z

gives you the advantage of preserving the cursor position.

Rook
  • 54,867
  • 44
  • 156
  • 233
11

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>

  • This is very useful, but to avoid having to press many keys I have mapped it to just CTRL-L, this is my map: inoremap ^L ^X^L – Jorge Gajon May 11 '09 at 06:38
10

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

Michael
  • 609
  • 5
  • 9
5

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

" set Y to duplicate lines, works in visual mode as well. nnoremap Y yyp vnoremap Y y`>pgv

Mwiza
  • 4,494
  • 2
  • 33
  • 30
Chris Penner
  • 1,835
  • 8
  • 14
5

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.

DarkWiiPlayer
  • 5,861
  • 2
  • 17
  • 29
3

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.

cori
  • 8,159
  • 7
  • 40
  • 77
2

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

ap-osd
  • 1,846
  • 12
  • 14
1

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

jedi
  • 1,480
  • 4
  • 17
  • 40
  • This leaves insert mode though, and just adding `i` to the end to re-enter it breaks `undo`, so the solution to duplicating lines in insert mode is not as trivial as it seems. – DarkWiiPlayer Apr 13 '18 at 07:30
  • This works perfectly fine for me: `imap Ypi` insert mode and `nmap Yp` in normal mode – jedi Apr 14 '18 at 17:48
0

I like to use this mapping:

:nnoremap yp Yp

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

yolenoyer
  • 7,117
  • 1
  • 20
  • 47
0

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
FrFernandez
  • 95
  • 1
  • 7