How to duplicate a whole line in Vim?

后端 未结 19 830
北海茫月
北海茫月 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 15:00

    yyp - paste after

    yyP - paste before

    0 讨论(0)
  • 2020-12-22 15:02

    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

    0 讨论(0)
  • 2020-12-22 15:02

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

    0 讨论(0)
  • 2020-12-22 15:03

    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.

    0 讨论(0)
  • 2020-12-22 15:05

    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
    
    0 讨论(0)
  • 2020-12-22 15:08

    Do this:

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

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