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
:ttakes a range on the left and a destination line number on the right:10,20t30copies lines 10 through 20 and places them after line 30:tis an alias for:copy— they are identical- The destination is always "after line N", so
:t0places 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 toyyp— 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,'bt0copies lines between marksaandbto the top of the file - Use with relative offsets:
:t+5copies the current line to 5 lines below the current position - The
:tcommand works with:gfor pattern-based duplication::g/TODO/t$copies every line containingTODOto the end of the file