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

How do I copy lines to a different location in the file without overwriting my yank register?

Answer

:t

Explanation

The :t command (short for :copy) copies addressed lines to a destination line number, leaving the unnamed register untouched. This is ideal when you want to duplicate or relocate lines while keeping a previously yanked value available for pasting elsewhere.

How it works

:t takes the form :[range]t[destination]:

  • The range specifies which lines to copy (defaults to current line)
  • The destination is a line address — 0 means before line 1, . means current line, $ means last line
  • Copied lines are inserted after the destination

Common patterns:

:t.          " duplicate current line (copy to just below itself)
:t0          " copy current line to the very top of the file
:t$          " copy current line to the very bottom
:5,10t20     " copy lines 5–10 to after line 20
:'a,'bt$     " copy lines between marks a and b to end of file

Example

File contents:

1: const API_URL = 'https://api.example.com';
2: const DB_URL  = 'postgres://localhost/app';
3:
4: // test config

With cursor on line 1, running :t$ produces:

1: const API_URL = 'https://api.example.com';
2: const DB_URL  = 'postgres://localhost/app';
3:
4: // test config
5: const API_URL = 'https://api.example.com';

The unnamed register (") is unchanged — any previously yanked text is still available with p.

Tips

  • :m (:move) works identically but moves rather than copies lines
  • Combine with visual selection: :'<,'>t$ copies all selected lines to the bottom
  • :t. is faster than yyp and doesn't pollute the unnamed register

Next

How do I run text I've yanked or typed as a Vim macro without recording it first?