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

How do I delete consecutive duplicate lines from a file using a single Ex command?

Answer

:g/\(.\+\)\n\1/d

Explanation

The :g command with a backreference pattern can detect and delete consecutive duplicate lines in one pass. The pattern \(.\+\)\n\1 captures any non-empty line, then uses a backreference to require the immediately following line to be identical. This preserves order — unlike :sort u which also removes duplicates but sorts the output.

How it works

  • :g/{pattern}/d — run d (delete) on every line matching the pattern
  • \(.\+\) — capture group matching any non-empty line content
  • \n — matches the newline at the end of the line (extends match to the next line)
  • \1 — backreference requiring the next line to be identical to the captured content

Vim's pattern engine supports multi-line matching, so :g can find a line that is immediately followed by an identical line and delete it.

Example

apple
banana
banana
cherry
cherry
cherry
date

After :g/\(.\+\)\n\1/d:

apple
banana
cherry
date

Note: For a run of 3 or more identical lines, one duplicate is removed per pass. Run the command again to remove all but the last copy, or use :%s/\(.\+\)\(\n\1\)\+/\1/ for a single-pass approach that collapses any number of duplicates.

Tips

  • This is order-preserving — use it when you cannot sort the file
  • The pattern only matches CONSECUTIVE duplicates; non-adjacent identical lines are unaffected
  • Combine with a range to limit scope: :'<,'>g/\(.\+\)\n\1/d removes consecutive dupes only in a selection
  • For case-insensitive matching, add \c at the start: :g/\c\(.\+\)\n\1/d

Next

How do I encode and decode JSON data in Vimscript for configuration and plugin development?