How do I duplicate a whole line in Vim in a similar way to Ctrl+D in IntelliJ IDEA/ Resharper or Ctrl+Alt+&
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.
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
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.
I like to use this mapping:
:nnoremap yp Yp
because it makes it consistent to use alongside the native YP
command.
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
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