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?
23 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
Shift + P to paste the copied or deleted text before the current line
-
160An excellent point. For some reason though, I find hitting y twice is faster for me than SHIFT-y Commented Oct 6, 2008 at 12:35
-
31@camflan I think the Y should be "copy from the cursor to the end"– Tzu ngCommented Jul 19, 2012 at 11:35
-
58and 2yy can be used to copy 2 lines (and for any other n) Commented Oct 9, 2012 at 10:33
-
7@nXqd: Yes, a big percentage of users maps
Y
toy$
. (Consistent withD
andC
; (but not Vi compatible (no one cares.))) That is even proposed in:help Y
. Commented Jan 19, 2014 at 2:48 -
15To copy two lines, it's even faster just to go
yj
oryk
, especially since you don't double up on one character. Plus,yk
is a backwards version that2yy
can't do, and you can put the number of lines to reach backwards iny9j
ory2k
, etc.. Only difference is that your count has to ben-1
for a total ofn
lines, but your head can learn that anyway.– zelkCommented Mar 9, 2014 at 13:29
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
-
3When 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.– BenoitCommented Jun 30, 2012 at 14:17 -
2For the record: when you type a colon (:) you go into command line mode where you can enter Ex commands. 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. Commented Jul 31, 2012 at 8:21
-
7Downvoted 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.– mjaggardCommented Dec 12, 2012 at 12:57
-
10@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.– BenoitCommented Dec 12, 2012 at 14:57
-
11
YP
or Yp
or yyp
.
-
Y
is usually remapped toy$
(yank (copy) until end of line (from current cursor position, not beginning of line)) though. With this line in.vimrc
::nnoremap Y y$
Commented Aug 22, 2013 at 23:31 -
2
Doesn't get any simpler than this! From normal mode:
yy
then move to the line you want to paste at and
p
-
45
-
15
-
-
Does LiceCap overlay keypresses like that? I don't see that setting...– RikkiCommented Apr 30 at 20:18
Do this:
First, yy to copy the current line, and then p to paste.
-
1Yes, 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.– user146043Commented Jan 9, 2014 at 10:56
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
-
4This one came first but it's duplicate is accepted: stackoverflow.com/a/73357/1438029 Commented Sep 8, 2016 at 16:03
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.
-
1
yyp - remember it with "yippee!"
Multiple lines with a number in between:
y7yp
-
207yy is equivalent to y7y and is probably easier to remember how to do.– graywhCommented Jan 4, 2009 at 21:25
-
12y7yp (or 7yyp) is rarely useful; the cursor remains on the first line copied so that p pastes the copied lines between the first and second line of the source. To duplicate a block of lines use 7yyP– NefrubyrCommented Jul 29, 2014 at 14:09
-
yyp - paste after
yyP - paste before
-
7Since the line is being duplicated, the end result of the content is the same. Commented Nov 6, 2013 at 17:42
-
6@A-B-B However, there is a miniature difference here - what line will your cursor land on.– MikkCommented Dec 4, 2015 at 9:09
I like: Shift+v (to select the whole line immediately and let you select other lines if you want), y, p
Another option would be to go with:
nmap <C-d> mzyyp`z
gives you the advantage of preserving the cursor position.
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 Commented May 11, 2009 at 6:38
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.
-
5↑↑ best answer here!– user5287133Commented Jun 3, 2019 at 22:05
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 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
-
1
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
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.
-
5
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).
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
-
This leaves insert mode though, and just adding
i
to the end to re-enter it breaksundo
, so the solution to duplicating lines in insert mode is not as trivial as it seems. Commented Apr 13, 2018 at 7:30 -
This works perfectly fine for me:
imap <S-C-d> <Esc>Ypi
insert mode andnmap <S-C-d> <Esc>Yp
in normal mode– jediCommented Apr 14, 2018 at 17:48
I like to use this mapping:
:nnoremap yp Yp
because it makes it consistent to use alongside the native YP
command.
If you want to duplicate a line just below, the answers above are correct.
Another common case would be if you are somewhere in your text, and you want to duplicate a line located far away in your buffer, out of sight, and you don't want to go there, for some reason.
In this case:
- begin typing a few letters of the beginning of the line that you want to duplicate;
- hit
Ctrl-X Ctrl-L
: this will bring a pop-up list similar to the autocompletion one (Ctrl-P and Ctrl-N
); - navigate up and down through this list with
Ctrl-L
andCtrl-N
until you get the line you wish; - hit
Enter
, and voilà.
I find this particularly handy when you write code.
#copy current line
usage:
press yy (to copy current line)
press p (to paste copied line)
#help&more:
https://www.youtube.com/watch?v=HMpB28l1sLc
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
Y
esP
lease. :)