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

How do I duplicate every line in a buffer so each line appears twice in place?

Answer

:g/^/t.

Explanation

The command :g/^/t. uses Vim's :global command to duplicate every line in the buffer — a creative combination of two simple Ex primitives that produces a surprisingly useful result.

How it works

  • :g/^/ — the global command matches every line because ^ (start-of-line) is present on every line
  • t. — short for :copy ., copies the matched line to immediately after the current line (. means "here")

Vim's :g marks all matching lines before executing any sub-commands, so insertions don't cause lines to be processed twice. Each original line gets a copy inserted right below it in a single pass.

Example

Before:

foo
bar
baz

After :g/^/t.:

foo
foo
bar
bar
baz
baz

Tips

  • Insert blank lines instead: :g/^/put _ inserts an empty line (from the black-hole register) after each line, producing a blank-line-separated file without duplicating content
  • Limit to a range: :'<,'>g/^/t. duplicates only the visually selected lines
  • Duplicate one line: Use :.t. (or yyp) to copy just the current line
  • The same technique works for any sub-command: :g/^/m 0 reverses all lines by moving each to the top

Next

How do I jump to a tag in Vim and automatically choose if there is only one match?