How do I copy a line to another location without using yank and paste?
Answer
:t {address}
Explanation
How it works
The :t command (short for :copy) copies one or more lines and places them below the specified address. The syntax is:
:[range]t {address}
Where {address} is the line number after which the copied lines should be placed. This is a powerful Ex command that avoids the yank-navigate-paste workflow entirely.
Common forms:
:t.copies the current line and places it below the current line (duplicates the line).:t0copies the current line to the top of the file (after line 0).:t$copies the current line to the end of the file.:t15copies the current line and places it below line 15.:5t.copies line 5 and places it below the current line.:5,10t$copies lines 5 through 10 to the end of the file.
Example
Suppose you have a configuration file and want to copy line 3 to below line 10:
:3t10
Or you are on a line you want to duplicate (similar to yyp):
:t.
Or copy a visual selection to below line 20:
- Select lines with
V. - Type
:(Vim auto-fills'<,'>) - Complete the command:
:'<,'>t20
Advantages over yank-paste
- Does not affect any registers. Your unnamed register and named registers remain untouched.
- Works across distant parts of the file without needing to navigate.
- Can specify exact source and destination with line numbers.
- The
:t.shorthand is the fastest way to duplicate a line without touching any register.