vimtricks.wiki Concise Vim tricks, one at a time.

How do I copy a range of lines to another location without yanking and pasting?

Answer

:10,20t30

Explanation

The :t command (short for :copy) duplicates lines from one location to another without touching any registers. This means your yank register stays intact — no accidental overwrites. It is one of the most underused yet powerful Ex commands in Vim.

How it works

  • :t takes a range on the left and a destination line number on the right
  • :10,20t30 copies lines 10 through 20 and places them after line 30
  • :t is an alias for :copy — they are identical
  • The destination is always "after line N", so :t0 places the copy at the very top of the file and :t$ places it at the very bottom

Common forms

:t.       " Duplicate the current line (same as yyp but without touching registers)
:t0       " Copy the current line to the top of the file
:t$       " Copy the current line to the bottom of the file
:5t.      " Copy line 5 to just below the current line
:10,20t$  " Copy lines 10-20 to the end of the file

Example

Given the text:

1: header
2: alpha
3: beta
4: gamma
5: footer

Running :2,4t5 copies lines 2-4 after line 5:

1: header
2: alpha
3: beta
4: gamma
5: footer
6: alpha
7: beta
8: gamma

Tips

  • Use :t. as a register-safe alternative to yyp — it duplicates the current line without clobbering the unnamed register
  • Use :m (move) instead of :t (copy) if you want to relocate lines rather than duplicate them
  • Works with visual selections: select lines, then :'<,'>t{dest} copies the selection to the destination
  • Combine with marks: :'a,'bt0 copies lines between marks a and b to the top of the file
  • Use with relative offsets: :t+5 copies the current line to 5 lines below the current position
  • The :t command works with :g for pattern-based duplication: :g/TODO/t$ copies every line containing TODO to the end of the file

Next

How do I edit multiple lines at once using multiple cursors in Vim?