How to duplicate a whole line in Vim?

后端 未结 19 806
北海茫月
北海茫月 2020-12-22 14:29

How do I duplicate a whole line in Vim in a similar way to Ctrl+D in IntelliJ IDEA/ Resharper or Ctrl+Alt+&

相关标签:
19条回答
  • 2020-12-22 14:43

    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.

    0 讨论(0)
  • 2020-12-22 14:46

    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

    0 讨论(0)
  • 2020-12-22 14:48

    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.

    0 讨论(0)
  • 2020-12-22 14:48

    I like to use this mapping:

    :nnoremap yp Yp
    

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

    0 讨论(0)
  • 2020-12-22 14:50

    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

    0 讨论(0)
  • 2020-12-22 14:52

    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

    0 讨论(0)
提交回复
热议问题