How to duplicate a whole line in Vim?

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

提交回复
热议问题